47.Nginx安装 默认虚拟主机 用户认证 域名重定向

阅读: 评论:0

47.Nginx安装 默认虚拟主机 用户认证 域名重定向

47.Nginx安装 默认虚拟主机 用户认证 域名重定向

12.6 Nginx安装

12.7 默认虚拟主机

12.8 Nginx用户认证

12.9 Nginx域名重定向

扩展

nginx rewrite四种flag .html 

 

 

 

 

 

12.6 Nginx安装:

 

~1. cd /usr/local/src

~2.wget .8.

~3.tar zxf nginx-1.8.

~4.cd /usr/local/nginx-1.8.

~5../configure --prefix=/usr/local/nginx (此处看需求需要哪种模块就要加上。后期会用到https,再来重新编译)

~6.make && make install

~7. vim /etc/init.d/nginx 创建启动脚本 复制实例中内容

(参考.d_nginx )

~8.chmod 755 /etc/init.d/nginx

~9.chkconfig --add nginx

~10.chkconfig nginx on

~11.cd /usr/local/nginx/conf/; f.bak 配置文件

~12.f 复制实例中内容

(参考.conf)

~13./usr/local/nginx/sbin/nginx -t

~14./etc/init.d/nginx start

~15. netstat -lntp |grep 80

 

测试php解析:

vi /usr/local/nginx/html/1.php 加入如下内容

<?php

echo "test php scripts.";

?>

curl localhost/1.php

 

 

 

 

 

 

 

 

 

 

实例:

[root@localhost ~]# cd /usr/local/src

[root@localhost src]# wget .8.

[root@localhost src]# tar -zxvf nginx-1.8.

[root@localhostsrc]# cd nginx-1.8.0/

[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx

Configuration summary+ using system PCRE library+ OpenSSL library is not used+ md5: using system crypto library+ sha1: using system crypto library+ using system zlib librarynginx path prefix: "/usr/local/nginx"nginx binary file: "/usr/local/nginx/sbin/nginx"nginx configuration prefix: "/usr/local/nginx/conf"nginx configuration file: "/usr/local/nginx/f"nginx pid file: "/usr/local/nginx/logs/nginx.pid"nginx error log file: "/usr/local/nginx/logs/error.log"nginx http access log file: "/usr/local/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"

[root@localhost nginx-1.8.0]# echo $?

0

[root@localhost nginx-1.8.0]# make

make[1]: 离开目录“/usr/local/src/nginx-1.8.0”
make -f objs/Makefile manpage
make[1]: 进入目录“/usr/local/src/nginx-1.8.0”
sed -e "s|%%PREFIX%%|/usr/local/nginx|" -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" -e "s|%%CONF_PATH%%|/usr/local/nginx/f|" -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" < man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/usr/local/src/nginx-1.8.0”

[root@localhost nginx-1.8.0]# echo $?

0

[root@localhost nginx-1.8.0]# make install

make -f objs/Makefile install
make[1]: 进入目录“/usr/local/src/nginx-1.8.0”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' 		|| mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' 		|| mv '/usr/local/nginx/sbin/nginx' 			'/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' 		|| mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'
test -f '/usr/local/nginx/pes' 		|| cp pes '/usr/local/nginx/conf'
cp pes '/usr/local/nginx/pes.default'
test -f '/usr/local/nginx/conf/fastcgi_params' 		|| cp conf/fastcgi_params '/usr/local/nginx/conf'
cp conf/fastcgi_params 		'/usr/local/nginx/conf/fastcgi_params.default'
test -f '/usr/local/nginx/f' 		|| cp f '/usr/local/nginx/conf'
cp f '/usr/local/nginx/f.default'
test -f '/usr/local/nginx/conf/uwsgi_params' 		|| cp conf/uwsgi_params '/usr/local/nginx/conf'
cp conf/uwsgi_params 		'/usr/local/nginx/conf/uwsgi_params.default'
test -f '/usr/local/nginx/conf/scgi_params' 		|| cp conf/scgi_params '/usr/local/nginx/conf'
cp conf/scgi_params 		'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/f' 		|| cp f '/usr/local/nginx/f'
cp f '/usr/local/nginx/f.default'
test -d '/usr/local/nginx/logs' 		|| mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' || 		mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' 		|| cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' || 		mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/usr/local/src/nginx-1.8.0”

[root@localhost nginx-1.8.0]# echo $?

0

[root@localhost nginx-1.8.0]# ls /usr/local/nginx/ 看下他的目录,很小,没有多少文件

conf html logs sbin

[root@localhost nginx-1.8.0]# ls /usr/local/nginx/conf conf下就是配置文件

conf:
f  fastcgi_params  koi-utf  koi-win  pes  f  scgi_params  uwsgi_params  win-utf/usr/local/nginx/conf:
f          fastcgi_params          koi-utf  pes          f          scgi_params          uwsgi_params          win-utf
f.default  fastcgi_params.default  koi-win  pes.default  f.default  scgi_params.default  uwsgi_params.default

[root@localhost nginx-1.8.0]#  ls /usr/local/nginx/conf conf

html:
50x.html  index.html/usr/local/nginx/html:
50x.html  index.html

下就是样例(index.html)

50x.html index.html

[root@localhost nginx-1.8.0]# ls /usr/local/nginx/logs  /logs下就是存放日志的

[root@localhost nginx-1.8.0]# ls /usr/local/nginx/sbin  /sbin下就是他的核心文件

nginx

[root@localhost nginx-1.8.0]# /usr/local/nginx/sbin/nginx -t  /-t也是支持的。查看是否有错

nginx: the configuration file /usr/local/nginx/f syntax is ok
nginx: configuration file /usr/local/nginx/f test is successful

下面给他创建配置文件,做启动脚本

[root@localhost nginx-1.8.0]# vim /etc/init.d/nginx 启动脚本中复制一下内容

#!/bin/bash
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx SettingsNGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/f"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
prog="Nginx"start()
{echo -n $"Starting $prog: "mkdir -p /dev/shm/nginx_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL
}   stop(){echo -n $"Stopping $prog: "rm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL
}   reload()
{echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL}     restart()
{start}   configtest(){$NGINX_SBIN -c $NGINX_CONF -treturn 0}   case "$1" in
start)start;;
stop)stop;;
reload)reload;;
restart)restart;;
configtest)configtest;;
*)   echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1
esacexit $RETVAL

[root@localhost nginx-1.8.0]# chmod 755 /etc/init.d/nginx

[root@localhost nginx-1.8.0]# chkconfig --add nginx

[root@localhost nginx-1.8.0]# chkconfig nginx on

[root@localhostconf]# cd /usr/local/nginx/conf/

[root@localhost conf]# ls 其实里面有一个.conf的配置文件,但是我们不用这个。重新设置一个

f          fastcgi_params          koi-utf  pes          f          scgi_params          uwsgi_params          win-utf
f.default  fastcgi_params.default  koi-win  pes.default  f.default  scgi_params.default  uwsgi_params.default

[root@localhost conf]# f.1 把自带的改个名字

[root@localhost conf]# f 在创建一个我们需要的,直接vim就可以了。

复制一下的:

user nobody nobody; 用来定义nginx的启动是哪个用户。其实就是这个进程的用户,比如去一个目录下读一个图片,那么是有哪个用户的身份去读的呢?就在这定义

worker_processes 2; 定义子进程有几个

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200; 定义nginx最多可以打开多少个文件

 

events

{

use epoll; 使用epoll模式

worker_connections 6000; 进程最多有多少个连接

}

 

http

{

pes;

default_type application/octet-stream;

server_names_hash_bucket_size 3526;

server_names_hash_max_size 4096;

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'

' $host "$request_uri" $status'

' "$http_referer" "$http_user_agent"';

sendfile on;

tcp_nopush on;

keepalive_timeout 30;

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

connection_pool_size 256;

client_header_buffer_size 1k;

large_client_header_buffers 8 4k;

request_pool_size 4k;

output_buffers 4 32k;

postpone_output 1460;

client_max_body_size 10m;

client_body_buffer_size 256k;

client_body_temp_path /usr/local/nginx/client_body_temp;

proxy_temp_path /usr/local/nginx/proxy_temp;

fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

fastcgi_intercept_errors on;

tcp_nodelay on;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 8k;

gzip_comp_level 5;

gzip_http_version 1.1;

gzip_types text/plain application/x-javascript text/css text/htm

application/xml;

 

server 每个server对应的一个虚拟主机。也就是默认的虚拟主机。跟Apache的VirtualHost类似后面也可以再继续加

{

listen 80; 监听80端口

server_name localhost; 域名

index index.html index.htm index.php;

root /usr/local/nginx/html; 网站的根目录

 

location ~ .php$ 用来解析php的部分

{

include fastcgi_params;

fastcgi_pass unix:/tmp/php-fcgi.sock; 我们用的是sock,所以这样写

#fastcgi_pass 127.0.0.1:9000 如果监听的是指定的。可以这样写你需要指定的IP就可以了

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

}

}

}

user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;events{use epoll; worker_connections 6000; }http{pes;default_type application/octet-stream;server_names_hash_max_size 4096;log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'' $host "$request_uri" $status'' "$http_referer" "$http_user_agent"';sendfile on;tcp_nopush on;keepalive_timeout 30;client_header_timeout 3m;client_body_timeout 3m;send_timeout 3m;connection_pool_size 256;client_header_buffer_size 1k;large_client_header_buffers 8 4k;output_buffers 4 32k;postpone_output 1460;client_max_body_size 10m;client_body_buffer_size 256k;client_body_temp_path /usr/local/nginx/client_body_temp;proxy_temp_path /usr/local/nginx/proxy_temp;fastcgi_temp_path /usr/local/nginx/fastcgi_temp;fastcgi_intercept_errors on;tcp_nodelay on;gzip on;gzip_min_length 1k;gzip_comp_level 5;gzip_types text/plain application/x-javascript text/css text/htmgzip_comp_level 5;gzip_types text/plain application/x-javascript text/css text/htmserver{listen 80; server_name localhost; index index.html index.htm index.php;root /usr/local/nginx/html; location ~ .php$ {  include fastcgi_params;fastcgi_pass unix:/tmp/php-fcgi.sock; #fastcgi_pass 127.0.0.1:9000 fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;}  } }

[root@localhost conf]# /usr/local/nginx/sbin/nginx -t 编辑好配置文件,检查有没有错

nginx: the configuration file /usr/local/nginx/f syntax is ok
nginx: configuration file /usr/local/nginx/f test is successful

[root@localhost conf]# /etc/init.d/nginx start

Starting nginx (via systemctl):                            [  确定  ]

[root@localhost conf]# ps aux |grep nginx

root(父进程一般是root) 4936 0.0 0.0 24880 788 ? Ss 00:11 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/f

nobody 4937 0.0 0.1 27324 3364 ? S 00:11 0:00 nginx: worker process

nobody 4938 0.0 0.1 27324 3364 ? S 00:11 0:00 nginx: worker process

root 4940 0.0 0.0 112720 980 pts/0 S+ 00:11 0:00 grep --color=auto nginx

root       3775  0.0  0.0  24884   788 ?        Ss   13:29   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/f
nobody     3776  0.0  0.3  27328  3352 ?        S    13:29   0:00 nginx: worker process
nobody     3777  0.0  0.3  27328  3608 ?        S    13:29   0:00 nginx: worker process
root       3779  0.0  0.0 112724   988 pts/0    R+   13:30   0:00 grep --color=auto nginx

[root@localhost conf]# vim /usr/local/nginx/html/1.php 建一个php测试一下

<?phpecho "test php scripts.";?>

 

[root@localhost conf]# curl localhost/1.php 我们在配置文件了设置了localhost的目录就是/usr/local/nginx/html,所以这里直接写localhost/1.php就可以了

test php scripts.[root@localhost conf]# 
# 这就说明解析成功了

 

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

12.7 默认虚拟主机:

 

 

上一节我们定义了默认虚拟主机配置文件,其实就是第一个。当然,以下是专门来定义默认虚拟主机的配置段:

~1.vim /usr/local/nginx/f //增加:(之间配置了service的,要先把service那些删掉,在增加)

include vhost/*.conf

~2.mkdir /usr/local/nginx/conf/vhost

~3.cd !$; f //加入如下内容

server

{

listen 80 default_server; // 有default_service这个标记的就是默认虚拟主机

server_name aaa;

index index.html index.htm index.php;

root /data/wwwroot/default;

}

~4.mkdir -p /data/wwwroot/default/

~5. echo “This is a default site.”>/data/wwwroot/default/index.html

~6./usr/local/nginx/sbin/nginx -t

~7./usr/local/nginx/sbin/nginx -s reload

~8.curl localhost

~9.curl -x127.0.0.1:80 123

 

 

 

实例:

 

[root@localhostconf]# vim /usr/local/nginx/f

 

http http包含的service,在最下面定义一下include

{

gzip_http_version 1.1;

gzip_types text/plain application/x-javascript text/css text/htm

application/xml;

include vhost/*.conf;

[root@localhost conf]# pwd

/usr/local/nginx/conf 定义的clude就是在这个目录下的

[root@localhost conf]# mkdir vhost 创建上面定义include的目录。就是在conf目录下的

[root@localhost conf]# f 创建一个比如叫f的文件。注意后缀名,我们定义了vhost下的所有.conf的文件,所以要加上.conf。复制上下面的:

erver

{

listen 80 default_server; 有这个的就是默认的虚拟主机

server_name aaa; 名字叫aaa

index index.html index.htm index.php; 指定索引页

root /data/wwwroot/default; 指定网站根目录

}

server{listen 80 default_server;server_name aaa;index index.html index.htm index.php;root /data/wwwroot/default; }

以上指定了网站的根目录,还要创建/data/wwwroot/default的这个目录

[root@localhost conf]# mkdir /data/wwwroot/default 创建default这个目录

[root@localhost conf]# cd /data/wwwroot/default cd到这个目录

[root@localhost default]# vim index.html vim一个index.html的文件

[root@localhost default]# /usr/local/nginx/sbin/nginx -t 检测一下

nginx: the configuration file /usr/local/nginx/f syntax is ok
nginx: configuration file /usr/local/nginx/f test is successful

[root@localhost default]# /usr/local/nginx/sbin/nginx -s reload 重新加载

[root@localhost vhost]# curl localhost

“This is a default site.” 不显示原因不明

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="/">nginx</a>.<br/>
Commercial support is available at
<a href="/">nginx</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>

[root@localhost vhost]# curl -x192.168.30.134:80 bbb

“This is a default site.” 不管访问的是什么,都是显示的这个。这个就叫默认虚拟主机

阿鑫在做的时候curl报错拒绝链接,发现没有在vhost的目录下f。需注意

 

我们在vhost里创建了f里面设定了default_service这个默认虚拟主机的标记。其实在vhost这个目录下可以创建很多.conf的文件(也就是虚拟主机),他也可以按顺序排列,放在第一位的就是默认虚拟主机。当然也可以通过名字,比如,aaa bbb ccc,那么aaa就是默认虚拟主机,当然这个也不是一个好的方法

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

 

12.8 Nginx用户认证:

 

 

需要定义一个用户密码认证的文件:

~1.vim /usr/local/nginx/conf/f//写入如下内容

server

{

listen 80;

server_name test;

index index.html index.htm index.php;

root /data/wwwroot/test;

location /

{

auth_basic "Auth"; 定义用户认证的名字

auth_basic_user_file /usr/local/nginx/conf/htpasswd; 用户名密码文件

}

}

~2.yum install -y httpd 安装httpd,为的是生成密码文件。要使用htpasswd这个命令

~3.htpasswd -c /usr/local/nginx/conf/htpasswd axin

~4.-t && -s reload //测试配置并重新加载

~5.mkdir /data/wwwroot/test

~6.echo “test”>/data/wwwroot/test/index.html

~7.curl -x127.0.0.1:80 test -I//状态码为401说明需要验证

~8.curl -uaming:passwd 访问状态码变为200

~9. 编辑windows的hosts文件,然后在浏览器中访问test会有输入用户、密码的弹窗

~~1.针对目录的用户认证(这个网站下的某一个目录,例如admin目录)。

vim /usr/local/nginx/conf/f 的location /后面加上admin目录就可以了

location /admin/

{

auth_basic "Auth";

auth_basic_user_file /usr/local/nginx/conf/htpasswd;

}

 

~~2. 访问admin下的php文件(单独一个文件)的时候做用户验证

vim /usr/local/nginx/conf/f 的location后面加上 ~admin.php

location ~ admin.php

{

auth_basic "Auth";

auth_basic_user_file /usr/local/nginx/conf/htpasswd;

}

 

 

实例:

[root@localhost ~]# cd /usr/local/nginx/conf/vhost/

[root@localhost vhost]# ls

f

[root@localhost vhost]# f

server

{

listen 80;

server_name test;

index index.html index.htm index.php;

root /data/wwwroot/test;

location /

{

auth_basic "Auth"; 定义用户认证的名字

auth_basic_user_file /usr/local/nginx/conf/htpasswd; 用户名密码文件

}

}

server{listen 80;server_name test;index index.html index.htm index.php;root /data/wwwroot/test;location /{auth_basic "Auth";auth_basic_user_file /usr/local/nginx/conf/htpasswd;}}

[root@localhost vhost]# yum install -y httpd

[root@localhost vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd axin  /-c代表生成密码文件和用户

New password: 
Re-type new password: 
Adding password for user axin

[root@localhost vhost]# htpasswd /usr/local/nginx/conf/htpasswd user1 不加-c是生成另外一个。加了-c会覆盖之前的用户

New password: 
Re-type new password: 
Adding password for user user1

[root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd 这就是我们生成的两个用户

axin:$apr1$65xVjybI$7J87ZKPixirDwtV6yISLz.
user1:$apr1$eNxB0qYf$L6O9tM5kb82fdvPk1UOQM1

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t 测试

nginx: the configuration file /usr/local/nginx/f syntax is ok
nginx: configuration file /usr/local/nginx/f test is successful

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload 重新加载(如果配置里有错的话,是不会生效的。不会破坏原来nginx的服务。如果直接restart的话,万一配置文件里有错误,一重启会导致服务停掉)最好是使用reload

[root@localhost vhost]# curl -x127.0.0.1:80 test 测试为401(用户认证,没有权限)

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>

[root@localhost vhost]# curl -uaxin:wangxin789 -x127.0.0.1:80 test 404(因为我们还没有创建他的根目录)

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>

[root@localhost vhost]# ls /data/wwwroot/ 看一下没有创建test

111 123.php abc default

[root@localhost vhost]# mkdir /data/wwwroot/test 创建test的目录

[root@localhost vhost]# echo "wozhenniu" > /data/wwwroot/test/index.html 给他创建一个index.html的文件,并输入点字符

[root@localhost vhost]# curl -uaxin:wangxin789 -x127.0.0.1:80 test 测试成功

wozhenniu

已上是针对整个网站做用户认证,下面是针对一个目录下做用户认证:

~~1.

[root@localhost vhost]# vim /usr/local/nginx/conf/f 打开.conf

server

{

listen 80;

server_name test;

index index.html index.htm index.php;

root /data/wwwroot/test;

location /admin/ 加入admin/

{

auth_basic "Auth";

auth_basic_user_file /usr/local/nginx/conf/htpasswd;

}

}

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/f syntax is ok
nginx: configuration file /usr/local/nginx/f test is successful

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# mkdir /data/wwwroot/test/admin/

[root@localhost vhost]# echo "wozhenni" > /data/wwwroot/test/admin/index.html

[root@localhost vhost]# curl -x127.0.0.1:80 test/admin/index.html -I

HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:24:23 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

[root@localhost vhost]# curl -uaxin:123456 -x127.0.0.1:80 test/admin/index.html -I

HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:25:01 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Tue, 23 Jul 2019 06:24:06 GMT
Connection: keep-alive
ETag: "5d36a806-9"
Accept-Ranges: bytes

[root@localhost vhost]# vim /usr/local/nginx/conf/f

server

{

listen 80;

server_name test;

index index.html index.htm index.php;

root /data/wwwroot/test;

location ~ admin.php 指定单独的.php文件

{

auth_basic "Auth";

auth_basic_user_file /usr/local/nginx/conf/htpasswd;

}

}

[root@localhost vhost]# vim /data/wwwroot/test/admin.php 创建这个admin.php

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/f syntax is oknginx: configuration file /usr/local/nginx/f test is successful

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# curl -x127.0.0.1:80 test/admin.php -I 测试显示401

HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:31:07 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

 

[root@localhost vhost]# curl -uaxin:123456 -x127.0.0.1:80 test/admin.php -I 指定用户200

HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:32:01 GMT
Content-Type: application/octet-stream
Content-Length: 0
Last-Modified: Tue, 23 Jul 2019 06:27:11 GMT
Connection: keep-alive
ETag: "5d36a8bf-0"
Accept-Ranges: bytes

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

12.9 Nginx域名重定向:

 

 

 

~1.更改f(vim /usr/local/nginx/conf/f)

~2.server

{

listen 80;

server_name test test1 test2;

index index.html index.htm index.php;

root /data/wwwroot/test;

if ($host != 'test' ) { 跟apache的域名跳转是一样的

rewrite ^/(.*)$ /$1 permanent;

}

}

~3.server_name后面支持写多个域名,这里要和httpd的做一个对比

~4.permanent为永久重定向,状态码为301。如果写redirect则为302。暂时重定向

 

 

实例:

[root@localhost vhost]# vim /usr/local/nginx/conf/f

server

{

listen 80;

server_name test test1 test2

index index.html index.htm index.php;

root /data/wwwroot/test;

if ($host != 'test' ) {

rewrite ^/(.*)$ /$1 permanent;

}

}

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/f syntax is ok

nginx: configuration file /usr/local/nginx/f test is successful

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# curl -x127.0.0.1:80 test1 -I 301,跳转到test

HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Tue, 23 Jul 2019 06:40:42 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: /

 

转载于:

本文发布于:2024-01-31 12:34:25,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170667566628553.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:虚拟主机   重定向   域名   用户   Nginx
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23