12.13 Nginx防盜鏈php
12.14 Nginx訪問控制css
12.15 Nginx解析php相關配置(502的問題)html
12.16 Nginx代理mysql
擴展linux
502問題彙總 http://ask.apelearn.com/question/9109nginx
location優先級 http://blog.lishiming.net/?p=100web
12.13 Nginx防盜鏈:sql
配置以下,能夠和上面的配置結合起來apache
vim /usr/local/nginx/conf/vhost/test.com.confvim
# 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@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
# 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;
}
}
[root@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axinlinux-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/2.gif 測試防盜鏈,要指定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
[root@axinlinux-01 ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/2.gif
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.159.128訪問過來,是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@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/
{
allow 192.168.159.128;
allow 127.0.0.1;
deny all;
}
[root@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axinlinux-01 ~]# curl -e "http://www.baidu.com" -x127.0.0.1:80 test.com/admin/1.html -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 15 Aug 2018 15:17:05 GMT
Content-Type: text/html
Content-Length: 13
Last-Modified: Wed, 15 Aug 2018 15:17:00 GMT
Connection: keep-alive
ETag: "5b7443ec-d"
Accept-Ranges: bytes
[root@axinlinux-01 ~]# curl -e "http://www.baidu.com" -x192.168.159.128:80 test.com/admin/1.html -I
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Wed, 15 Aug 2018 15:18:40 GMT
Content-Type: text/html
Content-Length: 13
Last-Modified: Wed, 15 Aug 2018 15:17:00 GMT
Connection: keep-alive
ETag: "5b7443ec-d"
Accept-Ranges: bytes
~~2.
[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
location ~ .*\.(js|css)$
{
expires 12h;
access_log off;
}
[root@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axinlinux-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.php -I
HTTP/1.1 403 Forbidden 爲403,被拒絕
Server: nginx/1.8.0
Date: Wed, 15 Aug 2018 15:35:39 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive
[root@axinlinux-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 200 OK 訪問1.txt就能夠。表明設置成功
~~3.
[root@axinlinux-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
[root@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axinlinux-01 ~]# 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
[root@axinlinux-01 ~]# curl -A "Tomatojlknkljn" -x127.0.0.1:80 test.com/upload/1.php -I -A模仿一個user_agent
HTTP/1.1 403 Forbidden 這時候被403了
Server: nginx/1.8.0
Date: Wed, 15 Aug 2018 15:51:05 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.159.128: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@axinlinux-01 ~]# 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
[root@axinlinux-01 ~]# 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;
}
[root@axinlinux-01 ~]# vim /data/wwwroot/test.com/1.php 先不reload,咱們先vim一個php,作測試
[root@axinlinux-01 ~]# curl -x192.168.159.128:80 test.com/1.php 發現並無解析phpinfo()
<?php
phpinfo();
[root@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axinlinux-01 ~]# curl -x192.168.159.128:80 test.com/1.php 再curl發現能夠解析了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
~2.
[root@axinlinux-01 ~]# 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@axinlinux-01 ~]# /etc/init.d/php-fpm reload 把php-fpm從新加載(也支持reload)
[root@axinlinux-01 ~]# /usr/local/php-fpm/sbin/php-fpm -t
[root@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axinlinux-01 ~]# 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@axinlinux-01 ~]# 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@axinlinux-01 ~]# /etc/init.d/php-fpm -t php-fpm測試和加載
Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status}
[root@axinlinux-01 ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@axinlinux-01 ~]# /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@axinlinux-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axinlinux-01 ~]# !curl php-fpm和Nginx所有修改以後,測試成功。解析成功
curl -x192.168.159.128:80 test.com/1.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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://121.201.9.155/; 真正的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;
}
}