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