12.13 Nginx防盜鏈php
12.14 Nginx訪問控制css
12.15 Nginx解析php相關配置(502的問題)html
12.16 Nginx代理mysql
擴展nginx
502問題彙總 http://ask.apelearn.com/question/9109web
location優先級 http://blog.lishiming.net/?p=100sql
12.13 Nginx防盜鏈:apache
配置以下,能夠和上面的配置結合起來vim
vim /usr/local/nginx/conf/vhost/test.com.confbash
# location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 把以前配置的過時時間註釋掉。在第二個location開始寫(由於一樣用到了location)
# {
# expires 7d;
# access_log off;
# }
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ ~*不區分大小寫
{
expires 7d; 過時時間是7天
valid_referers none blocked server_names *.test.com ; 關於防盜鏈的是這部分(意思是關於白名單的referer是什麼)
if ($invalid_referer) { 意思是若是不是白名單的,就會返回403
return 403;
}
access_log off; 訪問日誌是不記錄
}
實例:
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/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; } # 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/test.com.log; }
[root@localhost ~]# /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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/1.php 測試防盜鏈,要指定referer
HTTP/1.1 403 Forbidden 指定referer爲百度,跳轉過來就是403
Server: nginx/1.8.0
Date: Wed, 15 Aug 2018 14:44:38 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
HTTP/1.1 403 Forbidden Server: nginx/1.8.0 Date: Tue, 23 Jul 2019 09:24:14 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
[root@localhost ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.php
HTTP/1.1 200 OK 指定referer爲test.com跳轉過來就是200
Server: nginx/1.8.0
Date: Wed, 15 Aug 2018 14:43:29 GMT
Content-Type: image/gif
Content-Length: 19
Last-Modified: Tue, 14 Aug 2018 14:33:26 GMT
Connection: keep-alive
ETag: "5b72e836-13"
Expires: Wed, 22 Aug 2018 14:43:29 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12.14 Nginx訪問控制:
日常在運維網站的時候,常常會有一些請求不正常或是故意的去作一些限制,好比有一些機密的不想讓別人訪問。就能夠作一個白名單,只容許本身的公網IP或是公司的內部公網IP去訪問
~~1.
需求:訪問/admin/目錄的請求,只容許某幾個IP訪問,配置以下:
~1.vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/
{
allow 192.168.133.1; 這個IP容許。跟apache有點區別,沒有order。哪一個在前哪一個就優先生效。好比這個IP192.168.30.134訪問過來,是allow(容許),就到此爲止了,也就是容許的。不會再去執行下面的deny。而apache是誰在後最終執行的是哪個
allow 127.0.0.1; 這個IP容許
deny all; 其餘的所有deny(也就是以上兩個IP是容許的,其餘的都deny)
}
~2. mkdir /data/wwwroot/test.com/admin/
~3.echo 「test,test」>/data/wwwroot/test.com/admin/1.html
~4.-t && -s reload
~5.curl -x127.0.0.1:80 test.com/admin/1.html -I
~6.curl -x192.168.133.130:80 test.com/admin/1.html -I
~~2.
能夠匹配正則(也就是在能上傳圖片的目錄裏,禁止解析php):
~1.location ~ .*(upload|image)/.*\.php$ 只要是匹配upload的這個目錄,以php結尾的
{
deny all; 知足以上條件的,所有deny
}
~~3.
根據user_agent限制(防止cc攻擊。或是禁掉某些蜘蛛,不想被搜索掉,就能夠把一些網站封掉,沒有任何一個網站能爬到你的網站,就至關於你的網站被隱藏了同樣)
~1.if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato') 匹配(~)後面加*表明忽略大小寫
{
return 403;
}
~~4. deny all和return 403效果同樣(根據~~2與~~3裏的配置語句)
實例:
~~1.
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
{ 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; } # 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; } location /admin/ { allow 192.168.30.134; allow 127.0.0.1; deny all; } access_log /tmp/test.com.log; }
[root@localhost ~]# /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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -e "http://www.baidu.com" -x127.0.0.1:80 test.com/admin/index.html -I
HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Wed, 24 Jul 2019 10:15:14 GMT Content-Type: text/html Content-Length: 9 Last-Modified: Tue, 23 Jul 2019 06:24:06 GMT Connection: keep-alive ETag: "5d36a806-9" Accept-Ranges: bytes
[root@localhost ~]# curl -e "http://www.baidu.com" -x192.168.30.134:80 test.com/admin/index.html -I
HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Wed, 24 Jul 2019 10:18:43 GMT Content-Type: text/html Content-Length: 9 Last-Modified: Tue, 23 Jul 2019 06:24:06 GMT Connection: keep-alive ETag: "5d36a806-9" Accept-Ranges: bytes
~~2.
[root@localhost~]# vim /usr/local/nginx/conf/vhost/test.com.conf
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; } location /admin/ { allow 192.168.30.134; allow 127.0.0.1; deny all; } location ~ .*(upload|image)/.*\.php$ { deny all; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } access_log /tmp/test.com.log; }
[root@localhost ~]# /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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -x127.0.0.1:80 test.com/upload/1.php -I
HTTP/1.1 403 Forbidden Server: nginx/1.8.0 Date: Tue, 23 Jul 2019 09:40:08 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
[root@localhost ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 200 OK 訪問1.txt就能夠。表明設置成功
HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Wed, 24 Jul 2019 10:31:15 GMT Content-Type: text/plain Content-Length: 3 Last-Modified: Wed, 24 Jul 2019 10:27:32 GMT Connection: keep-alive ETag: "5d383294-3" Accept-Ranges: bytes
~~3.
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/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; } # 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; } location /admin/ { allow 192.168.30.134; allow 127.0.0.1; deny all; } location ~ .*(upload|image)/.*\.php$ { deny all; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; } access_log /tmp/test.com.log; }
[root@localhost ~]# /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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 200 OK 如今是能夠訪問的
Server: nginx/1.8.0
Date: Wed, 15 Aug 2018 15:49:40 GMT
Content-Type: text/plain
Content-Length: 9
Last-Modified: Wed, 15 Aug 2018 15:36:44 GMT
Connection: keep-alive
ETag: "5b74488c-9"
Accept-Ranges: bytes
HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Wed, 24 Jul 2019 10:31:15 GMT Content-Type: text/plain Content-Length: 3 Last-Modified: Wed, 24 Jul 2019 10:27:32 GMT Connection: keep-alive ETag: "5d383294-3" Accept-Ranges: bytes
[root@localhost ~]# curl -A "Tomatojlknkljn" -x127.0.0.1:80 test.com/upload/1.php -I -A模仿一個user_agent (不明白)
HTTP/1.1 403 Forbidden Server: nginx/1.8.0 Date: Tue, 23 Jul 2019 09:44:44 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12.15 Nginx解析php相關配置:
!!!注意:php-fpm配置文件中sock的定義是什麼,Nginx的sock就要是什麼。否則會502
配置以下:
~1.
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; 這個地方須要注意!!在cat /usr/local/php-fpm/etc/php-fpm.conf裏定義的「listen = /tmp/php-fcgi.sock」的路徑寫的是什麼,在如今的這個地址裏就要寫什麼,否則會502。也就是說,php-fpm定義的sock地址是什麼,nginx的sock就要是什麼,否則就會提示502
!還有一種可能會報502.是咱們以前在定義php-fpm的時候sock的下面一行是否是定義了listen.mode=666權限
!除了以上兩種,php-fpm的資源耗盡也會502。好比有個mysql查詢的很慢,卡死了,就要去優化了
(~2的實例是爲了證實php-fpm與Nginx的sock一致,此處與上面是總體的配置)
若是在php-fpm裏的listen的sock是IP,那麼這裏就要寫成 fastcgi_pass 192.168.30.134:9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
注意:這裏的路徑/data/wwwroot/test.com要和上面的root路徑對應起來
}
~3.
fastcgi_pass 用來指定php-fpm監聽的地址或者socket
實例:
~1.
[root@localhost ~]# cat /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock 首先查看sock的路徑
#listen = 127.0.0.1:9000
listen.mode = 666
user = php-fpm
group = php-fpm
[global] pid = /usr/local/php-fpm/var/run/php-fpm.pid error_log = /usr/local/php-fpm/var/log/php-fpm.log [www] listen = /tmp/php-fcgi.sock #listen = 127.0.0.1:9000 listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; 跟上面的sock路徑要是同樣的
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
index index.html index.htm index.php; root /data/wwwroot/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)$ { valid_referers none blocked server_names *.test.com ; if ($invalid_referer) { return 403; } access_log off; } location ~ .*\.(js|css)$ { location /admin/ allow 192.168.30.134; allow 127.0.0.1; deny all; } location ~ .*(upload|image)/.*\.php$ { deny all; } location ~ .*\.(js|css)$ { expires 12h; location /admin/ allow 192.168.30.134; allow 127.0.0.1; deny all; location ~ .*(upload|image)/.*\.php$ { deny all; server listen 80; index index.html index.htm index.php; rewrite ^/(.*)$ http://test.com/$1 permanent; # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ # { # access_log off; # } location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ expires 7d; if ($invalid_referer) { return 403; } access_log off; } location ~ .*\.(js|css)$ { expires 12h; access_log off; location /admin/ allow 192.168.30.134; allow 127.0.0.1; deny all; location ~ .*(upload|image)/.*\.php$ { deny all; } location ~ .*\.(js|css)$ { expires 12h; access_log off; } if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; } 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; } access_log /tmp/test.com.log; }
[root@localhost ~]# vim /data/wwwroot/test.com/1.php 先不reload,咱們先vim一個php,作測試
[root@localhost ~]# curl -x192.168.30.134:80 test.com/1.php 發現並無解析phpinfo()
<?php
phpinfo();
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t 這時候咱們在-t / -s reload
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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -x192.168.30.134:80 test.com/1.php 再curl發現能夠解析了
test php scripts.
~2.
[root@localhost ~]# vim /usr/local/php-fpm/etc/php-fpm.conf 咱們先更改php-fpm的sock監聽爲IP
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
#listen = /tmp/php-fcgi.sock 將以前的sock註釋掉
listen = 127.0.0.1:9000 改成IP端口通常就爲9000
[root@localhost ~]# /etc/init.d/php-fpm reload 把php-fpm從新加載(也支持reload)
[root@localhost ~]# /usr/local/php-fpm/sbin/php-fpm -t
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t 把Nginx也從新加載
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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# curl -x192.168.159.128:80 test.com/1.php 再來測試.php,就不能解析了
<html> <head><title>502 Bad Gateway</title></head> <body bgcolor="white"> <center><h1>502 Bad Gateway</h1></center> <hr><center>nginx/1.8.0</center> </body> </html>
[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ \.php$
{
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000; 記得加分號,阿鑫在作的時候忘加,-t的時候致使報錯
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
[root@localhost ~]# /etc/init.d/php-fpm -t php-fpm測試和加載
Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status}
[root@localhost ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t Nginx測試和加載
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 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# !curl php-fpm和Nginx所有修改以後,測試成功。解析成功
curl -x192.168.159.128:80 test.com/1.php
test php scripts.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12.16 Nginx代理:
好比,像訪問一個服務器可是這個服務器只有一個私網,這是不可能訪問到的。若是想訪問有一個辦法,有一箇中間者,這個中間者有一個特性,和web服務器能互通也能和用戶互通。那麼就能做爲Web服務器和用戶之間的一個代理者。那麼這個就是代理服務器,以下圖:
應用在用戶與Web服務器不能互通,或者互通太慢(好比訪問美國的網站)的場景
~1. cd /usr/local/nginx/conf/vhost 須要配置一個新的虛擬主機配置文件
~2.vim proxy.conf //加入以下內容 名字叫作 proxy.conf
server
{
listen 80;
server_name ask.apelearn.com; 定義域名
沒有root,由於是代理的,因此不須要
location /
{
proxy_pass http://61.153.96.141/; 真正的web服務器IP(也就是遠程服務端,Web服務器的IP)
proxy_set_header Host $host; 要訪問的域名是上面定義的server_name。也就是這裏的$host是上面的server_name
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
dig ask.apelearn.com
; <<>> DiG 9.9.4-RedHat-9.9.4-74.el7_6.1 <<>> ask.apelearn.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54835 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;ask.apelearn.com. IN A ;; ANSWER SECTION: ask.apelearn.com. 600 IN A 61.153.96.141 ;; AUTHORITY SECTION: apelearn.com. 5619 IN NS f1g1ns2.dnspod.net. apelearn.com. 5619 IN NS f1g1ns1.dnspod.net. ;; Query time: 169 msec ;; SERVER: 202.106.0.20#53(202.106.0.20) ;; WHEN: 四 7月 25 14:50:00 CST 2019 ;; MSG SIZE rcvd: 115
server { listen 80; server_name ask.apelearn.com; location / { proxy_pass http://61.153.96.141/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x127.0.0.1:80 ask.apelearn.com -I
HTTP/1.1 200 OK Server: nginx/1.8.0 Date: Thu, 25 Jul 2019 06:45:20 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.3.3 P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR" Set-Cookie: ape__Session=b51tdgniem3l580shh0rfsq8o1; path=/; domain=.apelearn.com Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache