12.7 Nginx默認虛擬主機
12.8 Nginx用戶認證
12.9 Nginx域名重定向
12.10 Nginx訪問日誌
12.11 Nginx日誌切割
12.12 靜態文件不記錄日誌和過時時間
12.13 Nginx防盜鏈
12.14 Nginx訪問控制
12.15 Nginx解析php相關配置
12.16 Nginx代理
12.7 Nginx默認虛擬主機
自定義默認虛擬主機
1、修改nginx配置文件
•
vim /usr/local/nginx/conf/nginx.conf
修改內容以下:
一、將http配置server部分刪掉
二、在http部分最後添加一句
include vhost/*.conf;
2、建立vhost目錄
•
mkdir /usr/local/nginx/conf/vhost
3、編輯自定義虛擬主機文件
cd /usr/local/nginx/conf/vhost
•
vim aaa.conf
#加入以下內容
server
{
listen 80
default_server;
#有這個標記的就是默認虛擬主機
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/aaa;
}
4、建立指定網站目錄
•
mkdir -p /data/wwwroot/aaa/
5、測試默認虛擬主機是否配置完成
一、添加內容到/data/wwwroot/default/index.html
•
echo 「This is a default site.」>/data/wwwroot/aaa/index.html
二、檢查配置文件語法並從新加載
•
/usr/local/nginx/sbin/nginx -t
• /usr/local/nginx/sbin/nginx -s reload
三、測試
•
curl localhost
• curl -x127.0.0.1:80 123.com
[root@xinlinux-03 aaa]#
curl localhost
「This is a default site.」
[root@xinlinux-03 aaa]#
curl -x127.0.0.1:80
123.com
「This is a default site.」
[root@xinlinux-03 aaa]#
curl -x127.0.0.1:80
hdiag.com
「This is a default site.」
兩種方法改成默認虛擬主機
一、在vhost目錄下第一個.conf的虛擬主機爲默認虛擬主機
二、定義虛擬主機時加上「
default_server」字段就是默認虛擬主機
12.8 Nginx用戶認證
1、自定義一個虛擬主機配置文件
•
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/wwwroot/test.com;
location
/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
2、生成密碼文件
•
yum install -y httpd
#若是已經安裝Apache則不用下載
•
/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
#使用htpasswd -c生成密碼文件到/usr/local/nginx/conf/htpasswd,並增長用戶xin
[root@xinlinux-03 aaa]#
/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
New password:
Re-type new password:
Adding password for user xin
3、測試配置並從新加載
•
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
•
mkdir /data/wwwroot/test.com
5、測試
•
echo 「test.com」>/data/wwwroot/test.com/index.html
•
curl -x127.0.0.1:80 test.com -I #狀態碼爲401說明須要驗證
[root@xinlinux-03 aaa]#
curl -x127.0.0.1:80
test.com
-I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:12:27 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
•
curl -uxin:1234 -x127.0.0.1:80
test.com
#訪問狀態碼變爲200
[root@xinlinux-03 aaa]#
curl -uxin:1234 -x127.0.0.1:80
test.com
• 編輯windows的hosts文件,而後在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗
•
針對目錄的用戶認證(修改location後面的內容)
location
/admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
HTTP/1.1
401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:46 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
HTTP/1.1
200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:10 GMT
Content-Type: application/octet-stream
Content-Length: 4
Last-Modified: Wed, 19 Sep 2018 09:17:52 GMT
Connection: keep-alive
ETag: "5ba21440-4"
Accept-Ranges: bytes
針對URL的用戶認證
location
~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
12.9 Nginx域名重定向
更改test.com.conf
server
{
listen 80;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
#if語句表示主域名爲test.com,將匹配的其餘域名跳轉到主域名
}
#server_name後面支持寫多個域名,這裏要和httpd的作一個對比
#permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302
HTTP/1.1
301 Moved Permanently
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:27:40 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
HTTP/1.1
404 Not Found
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:27:47 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
12.10 Nginx訪問日誌
配置日誌格式也在主配置文件
•
vim /usr/local/nginx/conf/nginx.conf
#搜索log_format
日誌文件設定參數格式 |
|
$remote_addr |
客戶端IP(公網IP) |
$http_x_forwarded_for |
代理服務器的IP |
$time_local |
服務器本地時間 |
$host |
訪問主機名(域名) |
$request_uri |
訪問的url地址 |
$status |
狀態碼 |
$http_referer |
referer |
$http_user_agent |
user_agent |
#除了在主配置文件nginx.conf裏定義日誌格式外,還須要在虛擬主機配置文件中增長
access_log /tmp/test.com.log combined_realip;
#這裏的combined_realip就是在nginx.conf中定義的日誌格式名字
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/test.com.log
127.0.0.1 - [19/Sep/2018:17:34:16 +0800]
test.com "/" 200 "-" "curl/7.29.0"
12.11 Nginx日誌切割
1、定義shell 腳本
•
vim /usr/local/sbin/nginx_logrotate.sh
#寫入以下內容
#! /bin/bash
# 假設nginx的日誌存放路徑爲/data/logs/
d=`date -d "-1 day" +%Y%m%d`
#日誌時間
logdir="
/usr/local/nginx
/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`
#生成新的日誌文件
2、添加一個任務計劃
crontab -e
0 0 * * * /bin/bash /usr/local/sbin/nginx_logrotate.sh
12.12 靜態文件不記錄日誌和過時時間
• 配置以下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 7d;
#定義過時時間
access_log off;
#靜態文件不記錄off
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
測試
cd /data/wwwroot/test.com
vim 1.gif
vim 2.js
curl -x127.0.0.1:80 test.com/1.gif
curl -x127.0.0.1:80 test.com/2.js
curl -x127.0.0.1:80 test.com/1.gifhaksdl
gahi
123giouh
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
127.0.0.1 - [19/Sep/2018:18:22:42 +0800]
test.com "/1.gifhaksdl" 404 "-" "curl/7.29.0"
12.13 Nginx防盜鏈
•配置以下,能夠和上面的配置結合起來
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 ;
#設置*.test.com的網站爲白名單
if ($invalid_referer) {
return 403;
}
access_log off;
}
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
測試
HTTP/1.1
403 Forbidden
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:26:48 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
curl -e "http://www.test.com" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1
200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:26:55 GMT
Content-Type: image/gif
Content-Length: 5
Last-Modified: Wed, 19 Sep 2018 09:42:00 GMT
Connection: keep-alive
ETag: "5ba219e8-5"
Expires: Wed, 26 Sep 2018 10:26:55 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
12.14 Nginx訪問控制
•需求:訪問/admin/目錄的請求,只容許某幾個IP訪問
1、編輯主配置文件 /usr/local/nginx/conf/nginx.conf
配置以下:
location /admin/
{
allow 192.168.233.150;
allow 127.0.0.1;
deny all;
#沒有order順序
}
2、測試
一、建立amdin目錄並添加內容到admin/1.html
•
mkdir /data/wwwroot/test.com/admin/
• echo 「test,test」>/data/wwwroot/test.com/admin/1.html
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
• curl -x127.0.0.1:80 test.com/admin/1.html -I
•curl -x192.168.233.150:80 test.com/admin/1.html -I
•能夠匹配正則(這段配置將/upload/目錄下全部.php結尾的deny掉)
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
HTTP/1.1
403 Forbidden
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:41:36 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
HTTP/1.1
200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:42:00 GMT
Content-Type: application/octet-stream
Content-Length: 0
Last-Modified: Wed, 19 Sep 2018 09:17:19 GMT
Connection: keep-alive
ETag: "5ba2141f-0"
Accept-Ranges: bytes
•根據user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
#~加上一個*號
"~*"能夠忽略大小寫
{
return 403;
}
#deny all和return 403效果同樣
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
curl -A "Tomatoafghra" -x127.0.0.1:80
test.com
-I
<html>
<head><title>
403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
12.15 Nginx解析php相關配置
配置虛擬主機配置文件
• 配置以下:
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
#fastcgi_pass 用來指定php-fpm監聽的地址或者socket,php-fpm定義的是什麼,nginx配置文件就要些什麼;若是
路徑寫錯會出現502狀態
#若是路徑改爲fastcgi_pass 127.0.0.1:9000則變成監聽端口
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
vim /data/wwwroot/test.com/1.php
<?php
echo "123456";
?>
<?php
echo "123456";
?>
[root@xinlinux-03
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
[root@xinlinux-03
test.com]#
/usr/local/nginx/sbin/nginx -s reload
實驗:將虛擬主機配置文件sock路徑寫錯漏了fc
一、-t並重載
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
HTTP/1.1
502 Bad Gateway
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 10:55:10 GMT
Content-Type: text/html
Content-Length: 172
Connection: keep-alive
三、查看錯誤日誌
tail /usr/local/nginx/logs/nginx_error.log
#發現提示找不到socket文件
四、更正虛擬主機配置的sock路徑,與php_fpm的sock路徑保持一致,而後在重試一次
HTTP/1.1
200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:00:56 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32
#另外一種502狀態
vim /usr/local/php-fpm/etc/php-fpm.conf
#若是監聽socket沒有在php配置文件定義sock文件的權限666,則會變成默認660權限,
狀態也會出現502
測試前:
[root@xinlinux-03
test.com]#
ll /tmp/php-fcgi.sock
srw-rw-rw- 1 root root 0 9月 19 16:59 /tmp/php-fcgi.sock
修改權限後:
[root@xinlinux-03
test.com]#
ll /tmp/php-fcgi.sock
srw-rw---- 1 root root 0 9月 19 19:20 /tmp/php-fcgi.sock
測試:
一、將php-fpm的sock文件權限去掉,-t並重載
/usr/local/pgp-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm restart
HTTP/1.1 502 Bad Gateway
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:25:57 GMT
Content-Type: text/html
Content-Length: 172
Connection: keep-alive
三、查看sock文件屬性
[root@xinlinux-03
test.com]#
ll /tmp/php-fcgi.sock
srw-rw---- 1 root root 0 9月 19 19:20 /tmp/php-fcgi.sock
#由nginx的配置文件能夠知道是nobody用戶啓動nginx的
[root@xinlinux-03
test.com]#
chown nobody /tmp/php-fcgi.sock
[root@xinlinux-03
test.com]#
ll /tmp/php-fcgi.sock
srw-rw---- 1 nobody root 0 9月 19 19:32 /tmp/php-fcgi.sock
HTTP/1.1
200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:34:36 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32
四、將權限改回去,-t並重載
/usr/local/pgp-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm restart
HTTP/1.1
200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 11:30:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.32
12.16 Nginx代理
•
cd /usr/local/nginx/conf/vhost
• vim proxy.conf
#加入以下內容
server
{
listen 80;
server_name ask.apelearn.com;
location /
{
proxy_pass http://121.201.9.155/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
curl -x127.0.0.1:80 ask.apelearn.com/roots.txt
12.17擴展
nginx.conf 配置詳解
nginx rewrite四種flag
12.18課堂筆記
1、LAMP與LNMP的主要差別
LAMP:php主要以模塊的形式存在做爲Apache的模塊(.so文件),Apache經過調用模塊去實現調用PHP,Apache處理的是非php的請求;php模塊就是解析php代碼的;
Apache也能夠調用php做爲一個獨立的服務,並不是必須以模塊的形式存在於Apache中
LNMP:在LNMP上是一個php-fpm服務
2、php安裝
#與LAMP的php安裝主要差別是,須要開啓php-fpm服務
--enble-fpm #這個編譯參數關係到sbin目錄是否存在
#每一個服務都會有一個普通用戶
#1024之內的端口服務必須用root用戶才能啓動
3、Nginx安裝
#防火牆容許訪問80端口
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
4、擴展
一、nginx.conf全局配置
user nobody;
定義運行nginx服務的用戶,還能夠加上組,如 user nobody nobody;
worker_processes 1;
定義nginx子進程數量,即提供服務的進程數量,
該數值建議和服務cpu核數保持一致。
除了能夠定義數字外,還能夠定義爲auto,表示讓系統自動調整。
error_log logs/error.log;
定義錯誤日誌的路徑,能夠是相對路徑(相對prefix路徑的),也能夠是絕對路徑。(
錯誤日誌級別默認爲error)
該配置能夠在此處定義,也能夠定義到http、server、location裏
error_log logs/error.log notice;
定義錯誤日誌路徑以及日誌級別.
錯誤日誌級別:常見的錯誤日誌級別有[debug|info|notice|warn|error|crit|alert|emerg],級別越高記錄的信息越少。
若是不定義默認是error
pid logs/nginx.pid;
定義nginx進程pid文件所在路徑,能夠是相對路徑,也能夠是絕對路徑。
worker_rlimit_nofile 100000;
定義nginx最多打開文件數限制。若是沒設置的話,這個值爲操做系統(ulimit -n)的限制保持一致。
把這個值設高,nginx就不會有「too many open files」問題了。
nginx.conf server部分配置
server{} 包含在http{}內部,每個server{}都是一個虛擬主機(站點)。
如下爲nginx.conf配置文件中server{}部分的內容。
server {
listen 80; //監聽端口爲80,能夠自定義其餘端口,也能夠加上IP地址,如,listen 127.0.0.1:8080;
server_name localhost; //定義網站域名,能夠寫多個,用空格分隔。
#charset koi8-r; //定義網站的字符集,通常不設置,而是在網頁代碼中設置。(出現亂碼多是charset koi8-r錯了)
#access_log logs/host.access.log main; //定義訪問日誌,能夠針對每個server(即每個站點)設置它們本身的訪問日誌。
##在server{}裏有不少location配置段
location / {
root html; //定義網站根目錄,目錄能夠是相對路徑也能夠是絕對路徑。
index index.html index.htm; //定義站點的默認頁。
}
#error_page 404 /404.html; //定義404頁面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; //當狀態碼爲500、50二、50三、504時,則訪問50x.html
location = /50x.html {
root html; //定義50x.html所在路徑
}
#
proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#定義訪問php腳本時,將會執行本location{}部分指令
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; //proxy_pass後面指定要訪問的url連接,用proxy_pass實現代理。
#}
#
pass the PHP scripts to FastCGI server listening on
127.0.0.1:9000
#
#
location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000; //定義FastCGI服務器監聽端口與地址,支持兩種形式,1 IP:Port, 2 unix:/path/to/sockt
#
fastcgi_index index.php;
#
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //定義SCRIPT_FILENAME變量,後面的路徑/scripts爲上面的root指定的目錄
#
include fastcgi_params; //引用prefix/conf/fastcgi_params文件,該文件定義了fastcgi相關的變量
#}
#
deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#
location ~ /\.ht { //訪問的url中,以/.ht開頭的,如,
www.example.com/.htaccess,會被拒絕,返回403狀態碼。
# deny all; //這裏的all指的是全部的請求。
#}
}
#
another virtual host using mix of IP-, name-, and port-based configuration##server {
# listen 8000; //監聽8000端口#
listen somename:8080; //指定
ip:port# server_name somename alias another.alias; //指定多個
server_name
# location / {
# root html;
# index index.html index.htm;
# }
#}
#
HTTPS server##server {
# listen 443 ssl; //監聽443端口,即
ssl
# server_name localhost;
### 如下爲ssl相關配置
# ssl_certificate cert.pem; //指定pem文件路徑
# ssl_certificate_key cert.key; //指定key文件路徑
# ssl_session_cache shared:SSL:1m; //指定session cache大小
#
ssl_session_timeout 5m; //指定session超時時間#
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //指定ssl協議#
ssl_ciphers HIGH:!aNULL:!MD5; //指定ssl算法#
ssl_prefer_server_ciphers on; //優先採起服務器算法#
location / {
# root html;
# index index.html index.htm;
# }
#}
events配置部分
worker_connections 1024;
定義每一個work_process同時開啓的最大鏈接數,即容許最多隻能有這麼多鏈接。
accept_mutex on;
當某一個時刻只有一個網絡鏈接請求服務器時,服務器上有多個睡眠的進程會被同時叫醒,這樣會損耗必定的服務器性能。
Nginx中的accept_mutex設置爲on,將會對多個Nginx進程(worker processer)接收鏈接時進行序列化,防止多個進程爭搶資源。
默認就是on。
multi_accept on;
nginx worker processer能夠作到同時接收多個新到達的網絡鏈接,前提是把該參數設置爲on。
默認爲off,即每一個worker process一次只能接收一個新到達的網絡鏈接。
use epoll;
Nginx服務器提供了多個事件驅動器模型來處理網絡消息。
其支持的類型有:select、poll、kqueue、epoll、rtsing、/dev/poll以及eventport。
* select:只能在Windows下使用,這個事件模型不建議在高負載的系統使用
* poll:Nginx默認首選,但不是在全部系統下均可用
* kqueue:這種方式在FreeBSD 4.1+, OpenBSD2.9+, NetBSD 2.0, 和 MacOS X系統中是最高效的
* epoll: 這種方式是在Linux 2.6+內核中最高效的方式
* rtsig:實時信號,可用在Linux 2.2.19的內核中,但不適用在高流量的系統中
* /dev/poll: Solaris 7 11/99+,HP/UX 11.22+, IRIX 6.5.15+, and Tru64 UNIX 5.1A+操做系統最高效的方式
* eventport: Solaris 10最高效的方式
http配置部分
MIME-Type
include mime.types; //cat conf/mime.types
定義nginx能識別的網絡資源媒體類型(如,文本、html、js、css、流媒體等)
default_type application/octet-stream;
定義默認的type,若是不定義改行,默認爲text/plain.
log_format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
其中main爲日誌格式的名字,後面的爲nginx的內部變量組成的一串字符串。
access_log logs/access.log main;
定義日誌的路徑以及採用的日誌格式,該參數能夠在server配置塊中定義。
sendfile on;
是否調用sendfile函數傳輸文件,默認爲off,使用sendfile函數傳輸,能夠減小user mode和kernel mode的切換,從而提高服務器性能。
對於普通應用設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。
sendfile_max_chunk 128k;
該參數限定Nginx worker process每次調用sendfile()函數傳輸數據的最大值,默認值爲0,若是設置爲0則無限制。
tcp_nopush on;
當tcp_nopush設置爲on時,會調用tcp_cork方法進行數據傳輸。
使用該方法會產生這樣的效果:當應用程序產生數據時,內核不會立馬封裝包,而是當數據量積累到必定量時纔會封裝,而後傳輸。這樣有助於解決網絡堵塞問題。
默認值爲on。舉例:快遞員收快遞、發快遞,包裹累積到必定量纔會發,節省運輸成本。
keepalive_timeout 65 60;
該參數有兩個值,第一個值設置nginx服務器與客戶端會話結束後仍舊保持鏈接的最長時間,單位是秒,默認爲75s。
第二個值能夠省略,它是針對客戶端的瀏覽器來設置的,能夠經過curl -I看到header信息中有一項Keep-Alive: timeout=60,若是不設置就沒有這一項。
第二個數值設置後,瀏覽器就會根據這個數值決定什麼時候主動關閉鏈接,Nginx服務器就不操心了。但有的瀏覽器並不承認該參數。
send_timeout
這個超時時間是發送響應的超時時間,即Nginx服務器向客戶端發送了數據包,但客戶端一直沒有去接收這個數據包。
若是某個鏈接超過send_timeout定義的超時時間,那麼Nginx將會關閉這個鏈接。
client_max_body_size 10m;
瀏覽器在發送含有較大HTTP包體的請求時,其頭部會有一個Content-Length字段,client_max_body_size是用來限制Content-Length所示值的大小的。
這個限制包體的配置不用等Nginx接收完全部的HTTP包體,就能夠告訴用戶請求過大不被接受。會返回413狀態碼。
例如,用戶試圖上傳一個1GB的文件,Nginx在收完包頭後,發現Content-Length超過client_max_body_size定義的值,
就直接發送413(Request Entity Too Large)響應給客戶端。
gzip on;
是否開啓gzip壓縮。
gzip_min_length 1k;
設置容許壓縮的頁面最小字節數,頁面字節數從header頭得content-length中進行獲取。默認值是20。建議設置成大於1k的字節數,小於1k可能會越壓越大。
gzip_buffers 4 16k;
設置系統獲取幾個單位的buffer用於存儲gzip的壓縮結果數據流。4 16k表明分配4個16k的buffer。
gzip_http_version 1.1;
用於識別 http 協議的版本,早期的瀏覽器不支持 Gzip 壓縮,用戶會看到亂碼,因此爲了支持前期版本加上了這個選項。
若是你用了Nginx反向代理並指望也啓用Gzip壓縮的話,因爲末端通訊是http/1.1,故請設置爲 1.1。
gzip_comp_level 6;
gzip壓縮比,1壓縮比最小處理速度最快,9壓縮比最大但處理速度最慢(傳輸快但比較消耗cpu)
gzip_types mime-type ... ;
匹配mime類型進行壓縮,不管是否指定,」text/html」類型老是會被壓縮的。
在conf/mime.conf裏查看對應的type。
示例:gzip_types text/plain application/x-javascript text/css text/html application/xml;
gzip_proxied any;
Nginx做爲反向代理的時候啓用,決定開啓或者關閉後端服務器返回的結果是否壓縮,匹配的前提是後端服務器必需要返回包含」Via」的 header頭。
如下爲可用的值:
off - 關閉全部的代理結果數據的壓縮
expired - 啓用壓縮,若是header頭中包含 "Expires" 頭信息
no-cache - 啓用壓縮,若是header頭中包含 "Cache-Control:no-cache" 頭信息
no-store - 啓用壓縮,若是header頭中包含 "Cache-Control:no-store" 頭信息
private - 啓用壓縮,若是header頭中包含 "Cache-Control:private" 頭信息
no_last_modified - 啓用壓縮,若是header頭中不包含 "Last-Modified" 頭信息
no_etag - 啓用壓縮 ,若是header頭中不包含 "ETag" 頭信息
auth - 啓用壓縮 , 若是header頭中包含 "Authorization" 頭信息
any - 無條件啓用壓縮
gzip_vary on;
和http頭有關係,會在響應頭加個 Vary: Accept-Encoding ,可讓前端的緩存服務器緩存通過gzip壓縮的頁面,例如,用Squid緩存通過Nginx壓縮的數據。
二、Nginx比Apache更高效原理
Apache工做模式:每個請求過來都是由一個進程或線程鏈接進行交互。
Nginx的工做模式:nginx採用了模塊化、事件驅動、異步、單線程及非阻塞的架構,並大量採用了多路複用及事件通知機制。在nginx中,鏈接請求由爲數很少的幾個僅包含一個線程的進程worker以高效的迴環(run-loop)機制進行處理,而每一個worker能夠並行處理數千個的併發鏈接及請求。
Nginx會按需同時運行多個進程:一個主進程(master)和幾個工做進程(worker),配置了緩存時還會有緩存加載器進程(cache loader)和緩存管理器進程(cache manager)等。全部進程均是僅含有一個線程,並主要經過「共享內存」的機制實現進程間通訊。主進程以root用戶身份運行,而worker、cache loader和cache manager均應以非特權用戶身份運行。
Nginx的請求機制
Nginx之因此能夠支持高併發,是由於Nginx用的是異步非阻塞的機制,而Nginx是靠事件驅動模型來實現這種機制的。
在Nginx的事件驅動模型下,客戶端發起的全部請求在服務端都會被標記爲一個事件,Nginx會把這些事件收集到「事件收集器」裏, 而後再把這些事件交給內核去處理。
阻塞與非阻塞:
阻塞與非阻塞發生在IO調度中,好比內核到磁盤IO。
一、
阻塞方式下,進
程/線程在獲取最終結果以前,被系統掛起了,也就是所謂的阻塞了,在阻塞過程當中該進程什麼都幹不了, 直到最終結果反饋給它時,它才恢復運行狀態。
二、
非阻塞方式下,進
程/線程在獲取最終結果以前,並無進入被掛起的狀態,而是該進程能夠繼續執行新的任務。 當有最終結果反饋給該進程時,它再把結果交給客戶端。
舉例:依然是酒店前臺接待預約酒席電話的案例。 此時角色再也不是前臺,而是她的查詢有無剩餘酒席的同事。若是是阻塞方式,該同事在查詢有無剩餘酒席的過程當中,須要傻傻地 等待酒店管理系統給他返回結果,在此期間不能作其餘事情。 若是是非阻塞,該同事在等待酒店管理系統給他返回結果這段時間,能夠作其餘事情,好比能夠通知前臺剩餘酒席的狀況。
同步與異步:
同步、異步發生在當客戶端發起請求後,服務端處理客戶端的請求時。
一、
同步機制,是指客戶端發送請求後,須要等待服務端(內核)返回信息後,再繼續發送下一個請求。
在同步機制中,全部的請求在服務器端獲得同步,即發送方和接收方對請求的處理步調是一致的。
二、
異步機制,是指客戶端發出一個請求後,不等待服務端(內核)返回信息,就繼續發送下一個請求。
在異步機制中,全部來自發送方的請求造成一個隊列,接收方處理完後再通知發送方。
舉例:一家酒店前臺,在旺季的高峯時間段會接不少預約酒席的電話。
若是是同步機制狀況下,前臺每接一個電話後先不掛掉電話,而是去查詢有無剩餘酒席,查到結果後,告訴客戶。
若是是異步機制狀況下,前臺每接一個預約電話直接回復客戶,一會回覆,此時前臺把查詢這件事情交給了另外的同事,
該前臺掛掉電話後,繼續處理其餘客戶的事情,當另外的同事查詢到結果後再通知給前臺,前臺再通知客戶。
三、CGI,FastCGI,PHP-CGI與PHP-FPM概念(瞭解進化過程)
Module方式:PHP Module加載方式;是用 LoadModule 來加載 php5_module,就是把php做爲apache的一個子模塊來運行。當經過web訪問php文件時,apache就會調用php5_module來解析php代碼。
缺點:併發性低;且把mod_php編進apache時,出問題時很難定位是php的問題仍是apache的問題。
CGI:CGI(Common Gateway Interface)全稱是「通用網關接口」,CGI就是專門用來和 web 服務器打交道的。web服務器收到用戶請求,就會把請求提交給cgi程序(如php-cgi),cgi程序根據請求提交的參數做應處理(解析php),而後輸出標準的html語句,返回給web服服務器,WEB服務器再返回給客戶端,這就是普通cgi的工做原理。
缺點是fork-and-execute模式
FastCGI:像是一個常駐(long-live)型的CGI,它
能夠一直執行着,只要激活後,不會每次都要花費時間去fork一次。它還支持分佈式的運算, 即 FastCGI 程序能夠在網站服務器之外的主機上執行,而且接受來自其它網站服務器來的請求。
PHP-CGI:就是PHP實現的自帶的FastCGI管理器。性能太差,並且也很麻煩不人性化,主要體如今:
1. php-cgi變動php.ini配置後,需重啓php-cgi才能讓新的php-ini生效,不能夠平滑重啓。
2. 直接殺死php-cgi進程,php就不能運行了。
PHP-FPM :是對於 FastCGI 協議的具體實現,克服了PHP-CGI的兩個問題,他負責管理一個進程池,來處理來自Web服務器的請求。目前,PHP5.3版本以後,PHP-FPM是內置於PHP的。
由於PHP-CGI只是個CGI程序,他本身自己只能解析請求,返回結果,不會進程管理。因此就出現了一些可以調度 php-cgi 進程的程序,好比說由lighthttpd分離出來的spawn-fcgi。一樣,PHP-FPM也是用於調度管理PHP解析器php-cgi的管理程序。
PHP-FPM經過生成新的子進程能夠實現php.ini修改後的平滑重啓。
12.7 Nginx默認虛擬主機
12.8 Nginx用戶認證
12.9 Nginx域名重定向
12.10 Nginx訪問日誌
12.11 Nginx日誌切割
12.12 靜態文件不記錄日誌和過時時間
12.13 Nginx防盜鏈
12.14 Nginx訪問控制
12.15 Nginx解析php相關配置
12.16 Nginx代理
12.7 Nginx默認虛擬主機
自定義默認虛擬主機
1、修改nginx配置文件
•
vim /usr/local/nginx/conf/nginx.conf
修改內容以下:
一、將http配置server部分刪掉
二、在http部分最後添加一句
include vhost/*.conf;
2、建立vhost目錄
•
mkdir /usr/local/nginx/conf/vhost
3、編輯自定義虛擬主機文件
cd /usr/local/nginx/conf/vhost
•
vim aaa.conf
#加入以下內容
server
{
listen 80
default_server;
#有這個標記的就是默認虛擬主機
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/aaa;
}
4、建立指定網站目錄
•
mkdir -p /data/wwwroot/aaa/
5、測試默認虛擬主機是否配置完成
一、添加內容到/data/wwwroot/default/index.html
•
echo 「This is a default site.」>/data/wwwroot/aaa/index.html
二、檢查配置文件語法並從新加載
•
/usr/local/nginx/sbin/nginx -t
• /usr/local/nginx/sbin/nginx -s reload
三、測試
•
curl localhost
• curl -x127.0.0.1:80 123.com
[root@xinlinux-03 aaa]#
curl localhost
「This is a default site.」
[root@xinlinux-03 aaa]#
curl -x127.0.0.1:80
123.com
「This is a default site.」
[root@xinlinux-03 aaa]#
curl -x127.0.0.1:80
hdiag.com
「This is a default site.」
兩種方法改成默認虛擬主機
一、在vhost目錄下第一個.conf的虛擬主機爲默認虛擬主機
二、定義虛擬主機時加上「
default_server」字段就是默認虛擬主機
12.8 Nginx用戶認證
1、自定義一個虛擬主機配置文件
•
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/wwwroot/test.com;
location
/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
2、生成密碼文件
•
yum install -y httpd
#若是已經安裝Apache則不用下載
•
/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
#使用htpasswd -c生成密碼文件到/usr/local/nginx/conf/htpasswd,並增長用戶xin
[root@xinlinux-03 aaa]#
/usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xin
New password:
Re-type new password:
Adding password for user xin
3、測試配置並從新加載
•
/usr/local/nginx/sbin/nginx-t
/usr/local/nginx/sbin/nginx -s reload
•
mkdir /data/wwwroot/test.com
5、測試
•
echo 「test.com」>/data/wwwroot/test.com/index.html
•
curl -x127.0.0.1:80 test.com -I #狀態碼爲401說明須要驗證
[root@xinlinux-03 aaa]#
curl -x127.0.0.1:80
test.com
-I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:12:27 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
•
curl -uxin:1234 -x127.0.0.1:80
test.com
#訪問狀態碼變爲200
[root@xinlinux-03 aaa]#
curl -uxin:1234 -x127.0.0.1:80
test.com
• 編輯windows的hosts文件,而後在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗
•
針對目錄的用戶認證(修改location後面的內容)
location
/admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
HTTP/1.1
401 Unauthorized
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:46 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
HTTP/1.1
200 OK
Server: nginx/1.8.0
Date: Wed, 19 Sep 2018 09:23:10 GMT
Content-Type: application/octet-stream
Content-Length: 4
Last-Modified: Wed, 19 Sep 2018 09:17:52 GMT
Connection: keep-alive
ETag: "5ba21440-4"
Accept-Ranges: bytes
針對URL的用戶認證
location
~ admin.php
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}