跟httpd相似,第一個被Nginx加載的虛擬主要就是默認主機。但和httpd不一樣的地方是,它還有一個配置用來標記默認虛擬主機。也就是說,若是沒有這個標記,第一個虛擬主機爲默認虛擬主機。php
修改主配置文件nginx.onf,在結束符號}上面加入一行配置,改寫以下:css
意思是,/usr/local/nginx/conf/vhost/下面的全部以.conf結尾的文件都會加載,這樣咱們就能夠把全部虛擬主機配置文件放到vhost目錄下面了。html
[root@zhangjin-120:~]#mkdir /usr/local/nginx/conf/vhost
[root@zhangjin-120:~]#cd /usr/local/nginx/conf/vhost
[root@zhangjin-120:/usr/local/nginx/conf/vhost]#vim default.conf
寫入以下內容:nginx
server { listen 80 default_server; #有這個標記的就是默認虛擬主機 server_name aaa.com; index index.html index.htm index.php; root /data/nginx/default; }
保存退出。apache
[root@zhangjin-120:/usr/local/nginx/conf/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@zhangjin-120:/usr/local/nginx/conf/vhost]#/usr/local/nginx/sbin/nginx -s reloadvim
[root@zhangjin-120:/usr/local/nginx/conf/vhost]#echo "default_server" > /data/nginx/default/index.html #建立索引頁
[root@zhangjin-120:/usr/local/nginx/conf/vhost]#curl -x127.0.0.1:80 aaa.com
default_server
[root@zhangjin-120:/usr/local/nginx/conf/vhost]#curl -x127.0.0.1:80 1234.com #訪問一個沒有定義過的域名,也會訪問到aaa.comdefault_server
windows
再來建立一個新的虛擬主機:後端
vim test.com.conf,寫入以下內容:瀏覽器
1 server 2 { 3 listen 80; 4 server_name test.com; 5 index index.html index.htm index.php; 6 root /data/nginx/test.com; 7 8 location / 9 { 10 auth_basic "Auth"; 11 auth_basic_user_file /usr/local/nginx/conf/htpasswd; 12 } 13 }
上面內容,auth_basic表示打開認證,auth_basic_user_file指定用戶密碼文件(前提是這個用戶密碼文件存在)。生成用戶密碼文件的工具須要藉助httpd的htpasswd。bash
而後安裝httpd#yum install -y httpd,也可使用以前編譯安裝的apache2.4
用curl命令來驗證:
狀態碼401說明,該網站須要驗證。
咱們打開windows的hosts文件,並加入一行:
IP test.com
而後在瀏覽器中訪問test.com,出現如圖所示的驗證對話框:
輸入用戶名和密碼就能夠訪問了。若是是針對某個目錄作用戶認證,須要修改locaton後面的路徑:
Nginx的域名重定向和httpd的相似:
更改test.com.conf文件:
server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/nginx/test.com; if ($host != 'test.com') { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
在Nginx配置中,server_name後面能夠跟多個域名,permanent爲永久重定向,至關於httpd的R=301。另外還有一個經常使用的redirect,至關於httpd的R=302。
咱們來測試下:
查看Nginx的日誌格式:
上圖中,combined_realip爲日誌格式的名字,後面能夠調用它;$remote_addr爲訪問網站的用戶的出口IP;$http_x_forwarded_for爲代理服務器的IP,若是使用了代理,則會記錄代理的IP;$time_local爲當前的時間;$host爲訪問的主機名;$request_uri爲訪問的URL地址;$status爲狀態碼;$http_referer爲referer地址;$http_user_agent爲user_agent。
編輯虛擬主機配置文件,指定訪問日誌的路徑:
使用access_log來指定日誌的存儲路徑,最後面指定日誌的格式名字,測試以下:
Nginx的日誌很簡單,不像httpd還有自帶的切割工具,要想切割Nginx日誌須要藉助系統的切割工具或自定義腳本。
如下腳本爲Nginx日誌的切割腳本:
#vim /usr/local/sbin/nginx_log_rotate.sh,並寫入如下內容:
#! /bin/bash ## 假設nginx的日誌存放路徑爲/data/logs/ d=`date -d "-1 day" +%Y%m%d` logdir="/data/logs" nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir for log in `ls *.log` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid` ##add cron #0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
保存後,還須要增長任務計劃:
[root@zhangjin-120:/usr/local/nginx/conf/vhost]#crontab -e
no crontab for root - using an empty one
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
編輯配置文件:
#vim test.com.conf
並寫入如下內容:
server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/nginx/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 7d; access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } access_log /tmp/1.log combined_realip; }
使用location~能夠指定對應的靜態文件,expires配置過時時間,而access_log配置爲off就能夠不記錄訪問日誌了。下面來測試下:
能夠看到Cache-control對應的時間大小,另外也能夠看一下訪問日誌:
能夠看到,剛纔訪問的js和jpg,都沒有記錄到訪問日誌中。
編輯配置文件:#vim test.com.conf,寫入以下內容:
server { listen 80; server { listen 80; index index.html index.htm index.php; root /data/nginx/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } # { index index.html index.htm index.php; root /data/nginx/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ # { # expires 7d; # access_log off; # } location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ { expires 7d; valid_referers none blocked server_names *.test.com ; if ($invalid_referer) { return 403; } access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } access_log /tmp/1.log combined_realip; }
測試:
能夠看到,不單單有過時時間,還有防盜鏈的功能。
和httpd同樣,Nginx也須要限制某些IP不能訪問或者只容許某些IP訪問,好比,咱們有個需求「使訪問admin目錄的請求中容許192.168.6.1和127.0.0.1訪問」,配置文件加入以下內容:
location /admin/
{
allow 192.168.6.1;
allow 127.0.0.1;
deny all;
}
而後測試一下:
配置文件中的IP也能夠爲IP段,好比能夠寫成allow 192.168.6.0/24。若是隻拒絕某幾個IP,就能夠寫成這樣:
location /admin/
{
deny 192.168.6.1;
deny 127.0.0.1;
}
若是是黑名單的形式,就不須要寫allow all了,由於默認就是容許全部。除了這種簡單地限制目錄外,也能夠根據正則匹配來限制:
location ~ .*(abc|image)/.*\.php$
{
deny all;
}
還能夠針對user_agent作一些限制:
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
其中,~爲匹配符號,只要user_agent中含有Spider/3.0或者YoudaoBot字符串的,都會被拒絕,return爲直接返回403狀態碼,也能夠把它替換爲deny all。
在LAMP中,PHP是做爲httpd的一個模塊出現的,只在PHP模塊被加載,那麼就能解析PHP腳本了。而在LNMP中,PHP是以一個服務(php-fpm)的形式存在的,首先要啓動php-fpm服務,而後Nginx再和php-fpm通訊。也就是說,處理PHP腳本解析的工做是由php-fpm來完成的,Nginx僅僅是一個「搬運工」,它把用戶的請求傳遞給php-fpm,php-fpm處理完成後把結果傳遞給Nginx,Nginx再把結果返回給用戶。那麼Nginx是如何和PHP聯繫起來的呢?
其實test.com.conf配置文件的內容就包含了PHP相關的配置:
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/nginx/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIIPT_FILENAME /data/nginx/test.com$fastcgi_script_name;
}
access_log /tmp/1.log combined_realip;
}
其中,fastcgi_pass用來指定php-fpm的地址,若是php-fpm監聽的是一個tcp:port的地址(好比127.0.0.0:9000),那麼也須要在這裏改爲fastcgi_pass 127.0.0.1:9000。這個地址必定要和php-fpm服務監聽的地址匹配,不然會報502錯誤。
factcgi_param SCRIPT_FILENAME後面跟的路徑爲該站點的根目錄,和前面定義的root那個路徑保持一致。若是這裏配置不對,訪問PHP頁面會出現404錯誤。
一家公司有不少臺服務器,爲了節省成本,不能爲全部服務器都分配公網IP,而若是一個沒有公網IP的服務器要提供Web服務,就能夠經過代理來實現。
下面來看一下Nginx的代理如何配置:
[root@zhangjin-120:~]#cd /usr/local/nginx/conf/vhost/
[root@zhangjin-120:/usr/local/nginx/conf/vhost]#vim proxy.conf
添加以下內容:
server { listen 80; server_name ask.apelearn.com; location / { proxy_pass http://47.104.7.242/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
proxy_pass指定要代理的域名所在的服務器IP,後面的3行爲定義發日後端Web服務器的請求頭,第二行必需要有,不然代理不成功,它表示後端Web服務器的域名和當前配置文件中的server_name保持一致,第三行和第四行能夠省略。配置文件保存後,從新加載Nginx服務並驗證。