12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证12.9 Nginx域名重定向

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]# ls
mysql-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/nginx
checking for OS
 + Linux 3.10.0-514.el7.x86_64 x86_64
checking 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 ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking 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_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_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 -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: 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 Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $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]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@aminglinux-02 conf]# mv nginx.conf nginx.conf.old
[root@aminglinux-02 conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.old       uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_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/htm
application/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 -t
nginx: 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 start
Starting 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=1
8月 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 -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4849/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1236/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2076/master
tcp6       0      0 :::3306                 :::*                    LISTEN      1691/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      1236/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2076/master

发现占用端口的就是nginx本身
只能“杀掉 ”nginx程序再试

root@aminglinux-02 111]# killall nginx
[root@aminglinux-02 111]# /etc/init.d/nginx start
Starting nginx (via systemctl):  Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
                                                           [  确定  ]
[root@aminglinux-02 111]# ps aux |grep nginx
root      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.conf
nobody    6624  0.0  0.1  22928  3216 ?        S    21:32   0:00 nginx: worker process
nobody    6625  0.0  0.1  22928  3216 ?        S    21:32   0:00 nginx: worker process
root      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 stop
Stopping nginx (via systemctl):                            [  确定  ]
[root@aminglinux-02 111]# service nginx start
Starting nginx (via systemctl):                            [  确定  ]
[root@aminglinux-02 111]#

这下就正常了

启动完成以后看一下进程

[root@aminglinux-02 111]# ps aux |grep nginx
root      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.conf
nobody    6763  0.0  0.1  22928  3212 ?        S    21:45   0:00 nginx: worker process
nobody    6764  0.0  0.1  22928  3212 ?        S    21:45   0:00 nginx: worker process
root      6769  0.0  0.0 112664   972 pts/1    R+   21:55   0:00 grep --color=auto nginx

测试一下访问情况

[root@aminglinux-02 ~]# curl localhost
<!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="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

测试一下是否支持PHP解析

[root@aminglinux-02 ~]# curl localhost/1.php
This 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 -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 16:15:02 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30

返回码是200 证明是通的

12.7 默认虚拟主机

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

[root@aminglinux-02 default]#vim /usr/local/nginx/conf/nginx.conf
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
{
    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]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default  scgi_params.default   vhost
fastcgi.conf.default  koi-utf                 mime.types.default  nginx.conf.old      uwsgi_params          win-utf
fastcgi_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/vhost

server
{
    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.html
This is the default site.

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

[root@aminglinux-02 default]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: 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 localhost
This is the default site.
[root@aminglinux-02 default]# ls
index.html

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

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

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

12.8 Nginx用户认证

配置Nginx用户认证
新建一个虚拟主机配置文件

[root@aminglinux-02 vhost]# vim test.com.conf
server
{
    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]# ls
default
[root@aminglinux-02 wwwroot]# mkdir test.com
[root@aminglinux-02 wwwroot]# ls
default  test.com
[root@aminglinux-02 test.com]# vim index.html
test.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 aming
New password:
Re-type new password:
Adding password for user aming
[root@aminglinux-02 vhost]# cat /usr/local/nginx/conf/htpasswd
aming:$apr1$45TTsuN4$9sOnkf8GUOVuCoWI2PcyL/

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

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

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

测试一下语法

[root@aminglinux-02 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: 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 -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 17:33:37 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"

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

使用指定用户测试

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 17:36:04 GMT
Content-Type: text/html
Content-Length: 9
Last-Modified: Thu, 10 Aug 2017 17:35:22 GMT
Connection: keep-alive
ETag: "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]# ls
1.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.php
admin-->test.com-->auth
[root@aminglinux-02 test.com]# curl -x127.0.0.1:80  test.com/admin/login.php -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 17:43:44 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com/admin/login.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 17:44:26 GMT
Content-Type: application/octet-stream
Content-Length: 24
Last-Modified: Thu, 10 Aug 2017 17:41:45 GMT
Connection: keep-alive
ETag: "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.html
This is the default site.
test.com    
[root@aminglinux-02 test.com]# ls
1.html  admin  index.html
[root@aminglinux-02 test.com]# curl -x127.0.0.1:80  test.com/1.html -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 17:50:19 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 -uaming:123123 test.com/1.html -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 17:50:42 GMT
Content-Type: text/html
Content-Length: 35
Last-Modified: Thu, 10 Aug 2017 16:57:21 GMT
Connection: keep-alive
ETag: "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 -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: 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 -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 18:05:41 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

可以查看到 location 到了“ http://test.com/index.html

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

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test3.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 18:07:42 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html

[root@aminglinux-02 test.com]# curl -x127.0.0.1:80 test3.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 10 Aug 2017 18:07:52 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值