博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证12.9 Nginx域名重定向
阅读量:6273 次
发布时间:2019-06-22

本文共 21591 字,大约阅读时间需要 71 分钟。

hot3.png

12.6 Nginx安装

准备工作 在对应的目录下,下载好所需的安装包

[root@aminglinux-02 ~]# cd /usr/local/src/[root@aminglinux-02 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz[root@aminglinux-02 src]# lsmysql-5.6.35-linux-glibc2.5-x86_64.tar.gz  nginx-1.12.1.tar.gz  php-5.6.30  php-5.6.30.tar.gz

解包

tar -zxvf nginx-1.12.1.tar.gz

配置初始化

[root@aminglinux-02 src]# cd nginx-1.12.1[root@aminglinux-02 nginx-1.12.1]# pwd/usr/local/src/nginx-1.12.1[root@aminglinux-02 nginx-1.12.1]# ./configure --prefix=/usr/local/nginxchecking for OS + Linux 3.10.0-514.el7.x86_64 x86_64checking for C compiler ... found + using GNU C compiler + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)checking for gcc -pipe switch ... foundchecking for -Wl,-E switch ... foundchecking for gcc builtin atomic operations ... foundchecking for C99 variadic macros ... foundchecking for gcc variadic macros ... foundchecking for gcc builtin 64 bit byteswap ... foundchecking for unistd.h ... foundchecking for inttypes.h ... found......  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@aminglinux-02 nginx-1.12.1]# echo $?0

初始化没有问题

开始编译
查看Nginx目录下的东西

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/conf  html  logs  sbin

配置文件目录

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/conf/fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_paramsfastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.defaultfastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf

样例目录

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/html/50x.html  index.html

日志目录

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/logs/

核心进程目录

[root@aminglinux-02 nginx-1.12.1]# ls /usr/local/nginx/sbin/nginx

支持-t 检查语法错误

[root@aminglinux-02 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

给Nginx创建启动脚本

vim /etc/init.d/nginx

启动脚本内容如下:

#!/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/conf/nginx.conf"NGINX_PID="/usr/local/nginx/logs/nginx.pid"RETVAL=0prog="Nginx"start(){echo -n $"Starting $prog: "mkdir -p /dev/shm/nginx_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL}stop(){echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL}reload(){echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL}restart(){stopstart}configtest(){$NGINX_SBIN -c $NGINX_CONF -treturn 0}case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1esacexit $RETVAL

更改配置文件权限

[root@aminglinux-02 nginx-1.12.1]# chmod 755 !$chmod 755 /etc/init.d/nginx

将nginx加入到服务列表里

chkconfig --add nginx

配置开启启动nginx服务

chkconfig nginx on

定义配置文件 默认conf目录下是有一个配置文件的,但是我们用自己的配置

[root@aminglinux-02 conf]# lsfastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_paramsfastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.defaultfastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf[root@aminglinux-02 conf]# mv nginx.conf nginx.conf.old[root@aminglinux-02 conf]# lsfastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.old       uwsgi_paramsfastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.defaultfastcgi_params        koi-win                 nginx.conf.default  scgi_params.default  win-utf

创建一个配置文件

[root@aminglinux-02 conf]# vim nginx.conf

配置内容如下

user nobody nobody;                                     // 定义启动nginx的用户worker_processes 2;                                     //定义子进程有几个error_log /usr/local/nginx/logs/nginx_error.log crit;   //错误日志pid /usr/local/nginx/logs/nginx.pid;                    // PID所在worker_rlimit_nofile 51200;                             //nginx最多可以打开文件的上限events{use epoll;                               //使用epoll模式worker_connections 6000;  // 进行的最大连接数}http{include mime.types;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/htmapplication/xml;server                                       //一个server 对应一个虚拟主机,定义这个,才能正常访问网站  下面一整段,就是一个默认的虚拟主机{listen 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;      //nginx通过这一行配置来调用php-fpm服务fastcgi_pass 127.0.0.1:9000;              //如果php-fpm监听的是9000端口,直接这样写配置即可fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;}}}

作为一个网站的服务,必须监听一个端口,默认监听的是80端口,假如没有配置 server 这个几行,那么nginx将识别不到监听端口,导致服务不可用

编译好配置,就使用 -t 检查一下是否有错

[root@aminglinux-02 conf]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok     // /usr/local/nginx/conf/nginx. nginx:配置文件配置语法好nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful   //   /usr/local/nginx/conf/nginx. nginx:配置文件配置测试是成功的

启动服务

[root@aminglinux-02 nginx]# service nginx startStarting nginx (via systemctl):  Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.                                                           [失败]

提示出错

之后尝试执行提示的命令 “systemctl status nginx.service ”

[root@aminglinux-02 111]# systemctl status nginx.service● nginx.service - SYSV: http service.   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)   Active: failed (Result: exit-code) since 四 2017-08-10 21:26:30 CST; 46s ago     Docs: man:systemd-sysv-generator(8)  Process: 6541 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)8月 10 21:26:28 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)8月 10 21:26:28 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)8月 10 21:26:29 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)8月 10 21:26:29 aminglinux-02 nginx[6541]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)8月 10 21:26:30 aminglinux-02 nginx[6541]: nginx: [emerg] still could not bind()8月 10 21:26:30 aminglinux-02 nginx[6541]: [失败]8月 10 21:26:30 aminglinux-02 systemd[1]: nginx.service: control process exited, code=exited status=18月 10 21:26:30 aminglinux-02 systemd[1]: Failed to start SYSV: http service..8月 10 21:26:30 aminglinux-02 systemd[1]: Unit nginx.service entered failed state.8月 10 21:26:30 aminglinux-02 systemd[1]: nginx.service failed.Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.

提示端口已经被占用

查看端口

[root@aminglinux-02 111]# netstat -lntpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program nametcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4849/nginx: mastertcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1236/sshdtcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2076/mastertcp6       0      0 :::3306                 :::*                    LISTEN      1691/mysqldtcp6       0      0 :::22                   :::*                    LISTEN      1236/sshdtcp6       0      0 ::1:25                  :::*                    LISTEN      2076/master

发现占用端口的就是nginx本身

只能“杀掉 ”nginx程序再试

root@aminglinux-02 111]# killall nginx[root@aminglinux-02 111]# /etc/init.d/nginx startStarting nginx (via systemctl):  Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.                                                           [  确定  ][root@aminglinux-02 111]# ps aux |grep nginxroot      6623  0.0  0.0  20484   624 ?        Ss   21:32   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnobody    6624  0.0  0.1  22928  3216 ?        S    21:32   0:00 nginx: worker processnobody    6625  0.0  0.1  22928  3216 ?        S    21:32   0:00 nginx: worker processroot      6628  0.0  0.0 112664   976 pts/1    R+   21:35   0:00 grep --color=auto nginx

这时发现启动成功了

但是有警告提示
再次执行提示的命令“systemctl daemon-reload ”

[root@aminglinux-02 111]# systemctl daemon-reload[root@aminglinux-02 111]# service nginx stopStopping nginx (via systemctl):                            [  确定  ][root@aminglinux-02 111]# service nginx startStarting nginx (via systemctl):                            [  确定  ][root@aminglinux-02 111]#

这下就正常了

启动完成以后看一下进程

[root@aminglinux-02 111]# ps aux |grep nginxroot      6762  0.0  0.0  20484   624 ?        Ss   21:45   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confnobody    6763  0.0  0.1  22928  3212 ?        S    21:45   0:00 nginx: worker processnobody    6764  0.0  0.1  22928  3212 ?        S    21:45   0:00 nginx: worker processroot      6769  0.0  0.0 112664   972 pts/1    R+   21:55   0:00 grep --color=auto nginx

测试一下访问情况

[root@aminglinux-02 ~]# curl localhostWelcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.

For online documentation and support please refer tonginx.org.

Commercial support is available atnginx.com.

Thank you for using nginx.

测试一下是否支持PHP解析

[root@aminglinux-02 ~]# curl localhost/1.phpThis is nginx test page.[root@aminglinux-02 ~]#[root@aminglinux-02 ~]# vim /usr/local/nginx/html/1.php[root@aminglinux-02 ~]# curl -x127.0.0.1:80 localhost/1.php -IHTTP/1.1 200 OKServer: nginx/1.12.1Date: Thu, 10 Aug 2017 16:15:02 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.6.30

返回码是200 证明是通的

12.7 默认虚拟主机

修改nginx.cnf配置,删除默认的虚拟主机配置,重新定义虚拟主机配置所在路径

[root@aminglinux-02 default]#vim /usr/local/nginx/conf/nginx.confuser 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{    include mime.types;    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;    include vhost/*.conf;   //新增这一行,定义默认虚拟主机的目录}

第一次配置的时候,出现提示 “ } ”这个所在行语法错误,一直找不到原因,最后重新粘贴配置文件以后,发现只是新增的“include vhost/*.conf ”配置最后没有添加“ ; ”分号结尾。

创建虚拟主机的目录

[root@aminglinux-02 conf]# pwd/usr/local/nginx/conf[root@aminglinux-02 conf]# mkdir vhost[root@aminglinux-02 conf]# lsfastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default  scgi_params.default   vhostfastcgi.conf.default  koi-utf                 mime.types.default  nginx.conf.old      uwsgi_params          win-utffastcgi_params        koi-win                 nginx.conf          scgi_params         uwsgi_params.default定义新增虚拟主机的配置[root@aminglinux-02 conf]# cd vhost/[root@aminglinux-02 vhost]# pwd/usr/local/nginx/conf/vhostserver{    listen 80 default_server;                            //有这个“default_server ”就是表示这是一个默认虚拟主机    server_name aaa.com;                              //指定主机名    index index.html index.htm index.php;     //指定索引页    root /data/wwwroot/default;                   //指定root的目录}

创建网站的根目录

[root@aminglinux-02 vhost]# mkdir /data/wwwroot[root@aminglinux-02 vhost]# mkdir /data/wwwroot/default[root@aminglinux-02 vhost]# cd !$cd /data/wwwroot/default[root@aminglinux-02 default]# ls

在“/data/wwwroot/default “下创建索引页

[root@aminglinux-02 default]# vim index.htmlThis is the default site.

编辑好之后检查一下是否有语法的错误

[root@aminglinux-02 default]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加载nginx服务(更改配置文件以后,要重新加载或者重启一下服务)

[root@aminglinux-02 default]# /usr/local/nginx/sbin/nginx -s reload

测试一下访问默认页

[root@aminglinux-02 default]# curl localhostThis is the default site.[root@aminglinux-02 default]# lsindex.html

因为修改了nginx.conf的配置,现在看到的默认索引页,是我们刚刚新增的vhost的虚拟主机的索引页了

定义默认虚拟主机的两种办法:

  1. 默认虚拟主机,是根据目录的第一个.conf了进行选择,所以只需要在vhost目录下依次创建就可以了,当然这种方法不智能
  2. 只需要在vhost目录的.conf配置文件内,加上一个“default_server ”即可,把当前的这个配置对应的网站设置为第一个默认虚拟主机

12.8 Nginx用户认证

配置Nginx用户认证

新建一个虚拟主机配置文件

[root@aminglinux-02 vhost]# vim test.com.confserver{    listen 80 ;    server_name test.com;    index index.html index.htm index.php;    root /data/wwwroot/test.com;location /                                                        //表示全站,都需要进行用户认证#location  /admin                                                 // 这个地方只要加上" /admin " 就变成 针对这个站点的“admin” 这个目录需要用户认证#location  ~ admin.php                                            //如果把这行这样写,就会变成,匹配 “ admin.php ”这个页面的时候才需要用户认证    {        auth_basic              "Auth";                           //定义用户认证的名字        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;    //用户名、密码文件    }}

虚拟机创建好之后,创建所需的目录及文件

[root@aminglinux-02 vhost]# cd /data/wwwroot/[root@aminglinux-02 wwwroot]# lsdefault[root@aminglinux-02 wwwroot]# mkdir test.com[root@aminglinux-02 wwwroot]# lsdefault  test.com[root@aminglinux-02 test.com]# vim index.htmltest.com

配置弄好了,需要生产密码文件

需要用到Apache生成密码文件的工具“ htpasswd ”
两种情况: 1.如果本机安装有Apache,可以直接到所在目录运行htpasswd进行生成 2.如果没有安装,直接“ yum install -y httpd ”安装,因为yum安装的,所以工具存放在/usr/bin/下,可以直接使用htpasswd

生成密码文件

[root@aminglinux-02 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd amingNew password:Re-type new password:Adding password for user aming[root@aminglinux-02 vhost]# cat /usr/local/nginx/conf/htpasswdaming:$apr1$45TTsuN4$9sOnkf8GUOVuCoWI2PcyL/

==关于htpasswd -c 命令 第一次创建的时候因为没有htpasswd这个文件,需要-c创建,第二使用的时候因为已经有这个htpasswd文件了,将不再需要-c 选项,如果还继续使用-c 这个选项,将会重置 htpasswd里的东西==

测试-c 是否会重置htpasswd文件

[root@aminglinux-02 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd user1New password:Re-type new password:Adding password for user user1[root@aminglinux-02 vhost]# !catcat /usr/local/nginx/conf/htpasswduser1:$apr1$I/VKfzgJ$KTWtCG4aZhrvLU69tgvhl1[root@aminglinux-02 vhost]# htpasswd  /usr/local/nginx/conf/htpasswd amingNew password:Re-type new password:Adding password for user aming[root@aminglinux-02 vhost]# !catcat /usr/local/nginx/conf/htpasswduser1:$apr1$I/VKfzgJ$KTWtCG4aZhrvLU69tgvhl1aming:$apr1$idtTK3wd$RLibX1IYqH1x.rc6VibVg1

测试一下语法

[root@aminglinux-02 vhost]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新加载服务

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

==重新加载服务的好处在于,如果配置里面出错,将不会生效;如果是直接使用restart,如果配置有错,将会直接影响到网站的运行==

测试 location / 针对整个站点进行用户认证

因为修改配置的时候做的配置,就是针对整个站点配置的,直接对域名进行curl即可

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test.com -IHTTP/1.1 401 UnauthorizedServer: nginx/1.12.1Date: Thu, 10 Aug 2017 17:33:37 GMTContent-Type: text/htmlContent-Length: 195Connection: keep-aliveWWW-Authenticate: Basic realm="Auth"

提示错误码401,需要进行认证,认证方式 Basic realm="Auth"

使用指定用户测试

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com -IHTTP/1.1 200 OKServer: nginx/1.12.1Date: Thu, 10 Aug 2017 17:36:04 GMTContent-Type: text/htmlContent-Length: 9Last-Modified: Thu, 10 Aug 2017 17:35:22 GMTConnection: keep-aliveETag: "598c995a-9"Accept-Ranges: bytes

测试 location /admin 针对目录

修改test.com.conf

location  /

更改为

location  /admin

到站点目录下加创建一个admin 的目录,为了方便测试,创建一测试页

[root@aminglinux-02 test.com]# pwd/data/wwwroot/test.com[root@aminglinux-02 test.com]# mkdir admin[root@aminglinux-02 test.com]# ls1.html  admin  index.html[root@aminglinux-02 test.com]# echo "admin-->test.com-->auth" > admin/login.php[root@aminglinux-02 test.com]# cat admin/login.phpadmin-->test.com-->auth[root@aminglinux-02 test.com]# curl -x127.0.0.1:80  test.com/admin/login.php -IHTTP/1.1 401 UnauthorizedServer: nginx/1.12.1Date: Thu, 10 Aug 2017 17:43:44 GMTContent-Type: text/htmlContent-Length: 195Connection: keep-aliveWWW-Authenticate: Basic realm="Auth"[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com/admin/login.php -IHTTP/1.1 200 OKServer: nginx/1.12.1Date: Thu, 10 Aug 2017 17:44:26 GMTContent-Type: application/octet-streamContent-Length: 24Last-Modified: Thu, 10 Aug 2017 17:41:45 GMTConnection: keep-aliveETag: "598c9ad9-18"Accept-Ranges: bytes

测试 location ~ 1.html 针对固定的URL

修改test.com.conf

location  /admin

更改为

location  ~ 1.html

创建所需的1.html

[root@aminglinux-02 test.com]# pwd/data/wwwroot/test.com[root@aminglinux-02 test.com]# vim 1.htmlThis is the default site.test.com    [root@aminglinux-02 test.com]# ls1.html  admin  index.html[root@aminglinux-02 test.com]# curl -x127.0.0.1:80  test.com/1.html -IHTTP/1.1 401 UnauthorizedServer: nginx/1.12.1Date: Thu, 10 Aug 2017 17:50:19 GMTContent-Type: text/htmlContent-Length: 195Connection: keep-aliveWWW-Authenticate: Basic realm="Auth"[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com/1.html -IHTTP/1.1 200 OKServer: nginx/1.12.1Date: Thu, 10 Aug 2017 17:50:42 GMTContent-Type: text/htmlContent-Length: 35Last-Modified: Thu, 10 Aug 2017 16:57:21 GMTConnection: keep-aliveETag: "598c9071-23"Accept-Ranges: bytes

12.9 Nginx域名重定向

在Nginx里“ server_name ” 支持跟多个域名;但是Apache“ server_name ”只能跟一个域名,需要跟多个域名,需要使用Alisa

在Nginx的conf配置文件里“server_name ” 设置了多个域名,就会使网站的权重变了,到底需要哪个域名为主站点呢
所以需要域名重定向
更改配置“test.com.conf ”文件

server{    listen 80 ;    server_name test.com;    index index.html index.htm index.php;    root /data/wwwroot/test.com; if ($host != 'test.com' )   //假如域名,“!=”不等于 test.com,将执行下面的脚本    {        rewrite  ^/(.*)$  http://test.com/$1  permanent;   //   ^/(.*)$  正式写法  http://$host/(.*)$ 这段可以直接省略掉的,同时还可以加上一些规则,“ permanent ”就是301的意思;如果想弄成302,只需要更改为“ redirect ”    }location ~ 1.html    {        auth_basic              "Auth";        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;    }}

改好配置,检查语法,重新加载服务

[root@aminglinux-02 test.com]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful(reverse-i-search)`s': /usr/local/nginx/^Cin/nginx -t[root@aminglinux-02 test.com]# /usr/local/nginx/sbin/nginx -s reload

测试test2.com/index.html的跳转情况

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test2.com/index.html -IHTTP/1.1 301 Moved PermanentlyServer: nginx/1.12.1Date: Thu, 10 Aug 2017 18:05:41 GMTContent-Type: text/htmlContent-Length: 185Connection: keep-aliveLocation: http://test.com/index.html

可以查看到 location 到了“ ”

测试test3.com/index.html的跳转情况

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test3.com/index.html -IHTTP/1.1 301 Moved PermanentlyServer: nginx/1.12.1Date: Thu, 10 Aug 2017 18:07:42 GMTContent-Type: text/htmlContent-Length: 185Connection: keep-aliveLocation: http://test.com/index.html[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test3.com/admin/index.html -IHTTP/1.1 301 Moved PermanentlyServer: nginx/1.12.1Date: Thu, 10 Aug 2017 18:07:52 GMTContent-Type: text/htmlContent-Length: 185Connection: keep-aliveLocation: http://test.com/admin/index.html

转载于:https://my.oschina.net/nova12315/blog/1784792

你可能感兴趣的文章
java读取excel、txt 文件内容,传到、显示到另一个页面的文本框里面。
查看>>
《从零开始学Swift》学习笔记(Day 51)——扩展构造函数
查看>>
python多线程队列安全
查看>>
[汇编语言学习笔记][第四章第一个程序的编写]
查看>>
android 打开各种文件(setDataAndType)转:
查看>>
补交:最最原始的第一次作业(当时没有选上课,所以不知道)
查看>>
Vue实例初始化的选项配置对象详解
查看>>
PLM产品技术的发展趋势 来源:e-works 作者:清软英泰 党伟升 罗先海 耿坤瑛
查看>>
vue part3.3 小案例ajax (axios) 及页面异步显示
查看>>
软件测试(二)之 Failure, Error & Fault
查看>>
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>
CSS基础知识(上)
查看>>
PHP中常见的面试题2(附答案)
查看>>
26.Azure备份服务器(下)
查看>>
mybatis学习
查看>>
LCD的接口类型详解
查看>>
C#中使用RabbitMQ收发队列消息
查看>>
Spring Boot Unregistering JMX-exposed beans on shutdown
查看>>