11月26日任務
12.6 Nginx安裝php
http://www.javashuo.com/article/p-kmiwnqmu-d.html
12.7 默認虛擬主機
12.8 Nginx用戶認證
12.9 Nginx域名重定向html
[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf # 刪除原有的server語句塊,替換爲下面的代碼 include vhost/*.conf;
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf [root@localhost conf]# mkdir vhost [root@localhost conf]# cd vhost/ [root@localhost vhost]# vim aaa.com.conf server { # 指定監聽80端口,並將該虛擬主機設置爲默認虛擬主機 listen 80 default_server; # 設置服務器的名稱 server_name aaa.com; # 設置服務器默認網頁 index index.html index.htm index.php; # 設置服務器的根目錄 root /data/www/default; }
[root@localhost vhost]# mkdir -p /data/www/default [root@localhost vhost]# cd /data/www/default/ [root@localhost default]# vim index.html aaa.com
[root@localhost 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@localhost default]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost default]# curl -x 127.0.0.1:80 aaa.com aaa.com # 因爲是默認的虛擬主機,任何域名均可以顯示默認網頁信息 [root@localhost default]# curl -x 127.0.0.1:80 bbb.com aaa.com
nginx中一個虛擬主機對於一個配置文件nginx
[root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf server { # 這個不是默認虛擬主機,default_server不須要配置 listen 80; server_name test.com; index index.html index.htm index.php; root /data/www/test.com; # 添加下列代碼 location / { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
[root@localhost default]# mkdir /data/www/test.com [root@localhost default]# vim /data/www/test.com/index.html test.com
[root@localhost default]# yum install -y httpd [root@localhost default]# htpasswd -c /usr/local/nginx/conf/htpasswd test New password: Re-type new password: Adding password for user test
[root@localhost 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@localhost default]# /usr/local/nginx/sbin/nginx -s reload
# 普通訪問 [root@localhost default]# curl -x 127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.2 Date: Sun, 31 Dec 2017 06:55:24 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" # 指定用戶訪問 [root@localhost default]# curl -x 127.0.0.1:80 -utest:1 test.com -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Sun, 31 Dec 2017 06:55:33 GMT Content-Type: text/html Content-Length: 8 Last-Modified: Sun, 31 Dec 2017 06:17:09 GMT Connection: keep-alive ETag: "5a4880e5-8" Accept-Ranges: bytes [root@localhost default]# curl -x 127.0.0.1:80 -utest:1 test.com test.com
[root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/www/test.com; # 修改location便可,其餘都不變 location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
[root@localhost 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@localhost default]# /usr/local/nginx/sbin/nginx -s reload
# test.com能夠訪問 [root@localhost default]# curl -x 127.0.0.1:80 test.com test.com # test.com下的admin目錄須要用戶認證 [root@localhost default]# curl -x 127.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.12.2</center> </body> </html>
*( 修改虛擬主機配置文件(使用~匹配文件)apache
[root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/www/test.com; # 修改location便可,其餘都不變,這裏匹配admin.php只是對簡單的表示 # 能夠使用更復雜的正則來顯示精準的文件認證 location ~ admin.php { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
[root@localhost 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@localhost default]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost default]# curl -x 127.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.12.2</center> </body> </html>
[root@localhost default]# vim /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; # nginx能夠配置多個主機名,apache只能使用ServerAlias來指定別名 server_name test.com test2.com; index index.html index.htm index.php; root /data/www/test.com; # 在多個域名 # 判斷host是否爲test.com if ($host != 'test.com') { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
[root@localhost 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@localhost default]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost default]# curl -x 127.0.0.1:80 test2.com/index.html <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [root@localhost default]# curl -x 127.0.0.1:80 test2.com/admin/index.html <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.12.2</center> </body> </html> [root@localhost default]# curl -x 127.0.0.1:80 test3.com/index.html aaa.com