12.6 Nginx安装
12.7 默认虚拟主机
12.8 Nginx用户认证
12.9 Nginx域名重定向
扩展
nginx.conf 配置详解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四种flag http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943php
[root@yong-01 ~]# cd /usr/local/src/
[root@yong-01 src]# wget http://nginx.org/download/nginx-1.4.7.tar.gz
[root@yong-01 src]# tar zxvf nginx-1.4.7.tar.gz
[root@yong-01 nginx-1.4.7]# ./configure --prefix=/usr/local/nginx
[root@yong-01 nginx-1.4.7]# make && make install
[root@yong-01 nginx-1.4.7]# ls /usr/local/nginx/ conf html logs sbin
[root@yong-01 nginx-1.4.7]# /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@yong-01 nginx-1.4.7]# chmod 755 /etc/init.d/nginx
[root@yong-01 nginx-1.4.7]# chkconfig --add nginx
[root@yong-01 nginx-1.4.7]# chkconfig nginx on
[root@yong-01 nginx-1.4.7]# cd /usr/local/nginx/conf/ [root@yong-01 conf]# ls fastcgi.conf koi-utf nginx.conf uwsgi_params fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default fastcgi_params mime.types scgi_params win-utf fastcgi_params.default mime.types.default scgi_params.default [root@yong-01 conf]# mv nginx.conf nginx.conf.bak
[root@yong-01 conf]# /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@yong-01 conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 肯定 ]
[root@yong-01 conf]# ps aux |grep nginx root 4036 0.0 0.0 24800 784 ? Ss 21:24 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 4037 0.0 0.1 27104 3364 ? S 21:24 0:00 nginx: worker process nobody 4038 0.0 0.1 27104 3364 ? S 21:24 0:00 nginx: worker process root 4043 0.0 0.0 112676 980 pts/0 R+ 21:24 0:00 grep --color=auto nginx
[root@yong-01 conf]# 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; } ………………
[root@yong-01 conf]# vim /usr/local/nginx/html/1.php <?php echo "hello nginx."; [root@yong-01 conf]# curl localhost/1.php hello nginx.
server { listen 80 default_server; // 有这个标记的就是默认虚拟主机 server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; }
[root@yong-01 conf]# mkdir /usr/local/nginx/conf/vhost
[root@yong-01 conf]# cd !$ cd /usr/local/nginx/conf/vhost
[root@yong-01 vhost]# vim aaa.com.conf server { listen 80 default_server; server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; }
[root@yong-01 vhost]# mkdir -p /data/wwwroot/default/
[root@hanfeng vhost]# cd /data/wwwroot/default/ [root@yong-01 default]# vim index.html This is a test default site.
[root@yong-01 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
[root@yong-01 default]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 default]# curl localhost This is a test default site. [root@yong-01 default]# curl localhost:80 bbb.com This is a test default site.
由于修改了nginx.conf的配置,如今看到的默认索引页,是咱们刚刚新增的vhost的虚拟主机的索引页了 定义默认虚拟主机的两种办法: 1.默认虚拟主机,是根据目录的第一个.conf了进行选择,因此只须要在vhost目录下依次建立就能够了,固然这种方法不智能 2.只须要在vhost目录的.conf配置文件内,加上一个“default_server ”便可,把当前的这个配置对应的网站设置为第一个默认虚拟主机html
server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
[root@yong-01 default]# cd /usr/local/nginx/conf/vhost/
[root@yong-01 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; //用户名密码文件 } } 保存退出
/usr/local/apache2/bin/htpasswd
[root@yong-01 vhost]# /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd yueyong New password: Re-type new password: Adding password for user yueyong
[root@yong-01 vhost]# cat /usr/local/nginx/conf/htpasswd yueyong:$apr1$2z3VXNaH$ACdIVwH0mjC7f92wip8AG0
[root@yong-01 vhost]# /usr/local/apache2/bin/htpasswd /usr/local/nginx/conf/htpasswd user1 New password: Re-type new password: Adding password for user user1 [root@yong-01 vhost]# cat /usr/local/nginx/conf/htpasswd yueyong:$apr1$2z3VXNaH$ACdIVwH0mjC7f92wip8AG0 user1:$apr1$MRSdQqmY$ou3wQ.ZdYU70WVfvntg6u.
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.4.7</center> </body> </html>
[root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.4.7</center> </body> </html>
[root@yong-01 vhost]# mkdir /data/wwwroot/test.com [root@yong-01 vhost]# echo "test.com" > /data/wwwroot/test.com/index.html [root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com test.com
[root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com/admin/ <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.4.7</center> </body> </html>
[root@yong-01 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 /admin/ // 后加上admin/ 目录 { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com test.com
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.4.7</center> </body> </html>
[root@yong-01 vhost]# mkdir /data/wwwroot/test.com/admin [root@yong-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.4.7</center> </body> </html> [root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com/admin/ test.com admin dir
[root@yong-01 vhost]# vim test.com.conf 在 location 后加~ admin.php便可 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location ~ admin.php { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ test.com admin dir [root@yong-01 vhost]# echo "test admin.php" > /data/wwwroot/test.com/admin.php [root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.4.7</center> </body> </html> 加上用户名和密码就能够正常访问了 [root@yong-01 vhost]# curl -uyueyong:123456 -x127.0.0.1:80 test.com/admin.php test admin.php
server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
[root@yong-01 vhost]# vim test.com.conf server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
检查配置文件语法错误,并从新加载配置文件linux
[root@yong-01 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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Thu, 07 Jun 2018 14:46:00 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/index.html [root@yong-01 vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Thu, 07 Jun 2018 14:46:21 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/admin/index.html
[root@yong-01 vhost]# curl -x127.0.0.1:80 test5.com/admin/index.html -I HTTP/1.1 404 Not Found Server: nginx/1.4.7 Date: Thu, 07 Jun 2018 14:47:19 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive