十二週四次課php
12.13Nginx防盜鏈css
12.14Nginx訪問控制html
12.15Nginx解析php相關配置mysql
12.16Nginx代理linux
12.13Nginx防盜鏈nginx
Nginx防盜鏈目錄概要web
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ajax
{sql
expires 7d;數據庫
valid_referers none blocked server_names *.test.com ;
if ($invalid_referer) {
return 403;
}
access_log off;
}
1.打開配置文件 vim /usr/local/nginx/conf/vhost/test.com.conf
[root@tianqi-01 local]# 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; //過時時間7天
valid_referers none blocked server_names *.test.com ; //定義一個白名單,referer就是指一些域名
if ($invalid_referer) { //若是不是白名單裏的
return 403; //返回403
}
access_log off;
}
server
{
listen 80;
server_name test.com test2.com test3.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 combined_realip;
}
保存退出
2.添加的配置中的 ~* 表示不區分大小寫,另外防盜鏈的配置裏面server_names能夠不寫照樣
3.檢查配置文件語法錯誤,並從新加載配置文件
[root@tianqi-01 local]# /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@tianqi-01 local]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 local]#
4.測試
[root@tianqi-01 local]# curl -x127.0.0.1:80 -I test.com/2.gif
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 13:02:00 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@tianqi-01 local]#
[root@tianqi-01 local]# curl -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 12:29:06 GMT
Content-Type: image/gif
Content-Length: 10
Last-Modified: Tue, 13 Mar 2018 13:30:40 GMT
Connection: keep-alive
ETag: "5aa7d280-a"
Expires: Wed, 21 Mar 2018 12:29:06 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
[root@tianqi-01 local]#
5.測試防盜鏈,使用curl -e
[root@tianqi-01 local]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 403 Forbidden
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 12:29:46 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@tianqi-01 local]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 12:29:55 GMT
Content-Type: image/gif
Content-Length: 10
Last-Modified: Tue, 13 Mar 2018 13:30:40 GMT
Connection: keep-alive
ETag: "5aa7d280-a"
Expires: Wed, 21 Mar 2018 12:29:55 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
[root@tianqi-01 local]#
//這說明防盜鏈配置成功了
6.查看日誌文件
[root@tianqi-01 local]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:36:51 +0800] test.com "/2.jsagasg" 404 "-" "curl/7.29.0"
[root@tianqi-01 local]#
12.14Nginx訪問控制
•需求:訪問/admin/目錄的請求,只容許某幾個IP訪問,配置以下:
location /admin/
{
allow 192.168.133.1;
allow 127.0.0.1;
deny all;
}
• mkdir /data/wwwroot/test.com/admin/
• echo 「test,test」>/data/wwwroot/test.com/admin/1.html
• -t && -s reload
• curl -x127.0.0.1:80 test.com/admin/1.html -I
• curl -x192.168.133.130:80 test.com/admin/1.html -I
• 能夠匹配正則
location ~ .*(abc|image)/.*\.php$
{
deny all;
}
•根據user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
• deny all和return 403效果同樣
1.編輯配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
location /admin/
{
allow 192.168.133.1;
allow 127.0.0.1;
deny all;
}
//假設訪問的目錄是admin,作一個限制
server
{
listen 80;
server_name test.com test2.com test3.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 127.0.0.1;
allow 192.168.11.136;
deny all;
}
access_log /tmp/test.com.log combined_realip;
}
保存退出
//在Apache中定義allow和deny是有前後順序的,例如上一個先allow再deny的話,全部的都不能過去;而Nginx的話,只有匹配了第一項,其餘的就再也不匹配。因此,Nginx的設置的效果就是以上兩個IP經過,其它的不經過。
2.檢查配置文件是否存在語法錯誤,並從新加載配置文件
[root@tianqi-01 local]# /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@tianqi-01 local]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 local]#
3.測試
[root@tianqi-01 local]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 13:20:51 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Mon, 12 Mar 2018 13:43:21 GMT
Connection: keep-alive
ETag: "5aa683f9-13"
Accept-Ranges: bytes
[root@tianqi-01 local]# curl -x192.168.11.136:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 13:22:10 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Mon, 12 Mar 2018 13:43:21 GMT
Connection: keep-alive
ETag: "5aa683f9-13"
Accept-Ranges: bytes
[root@tianqi-01 local]#
[root@tianqi-01 local]# curl -x192.168.11.139:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 13:23:03 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Mon, 12 Mar 2018 13:43:21 GMT
Connection: keep-alive
ETag: "5aa683f9-13"
Accept-Ranges: bytes
4.查看日誌
[root@tianqi-01 local]# !cat
cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:36:51 +0800] test.com "/2.jsagasg" 404 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:21:20:51 +0800] test.com "/admin/" 200 "http://www.baidu.com/1.txt" "curl/7.29.0"
192.168.11.136 - [14/Mar/2018:21:22:10 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
192.168.11.136 - [14/Mar/2018:21:23:03 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
[root@tianqi-01 local]#
//查看日誌文件之後,會看到訪問的192.168.11.139的來源IP也是192.168.11.136,由於它是被容許的,在白名單以內,因此顯示狀態碼爲200
5.查看IP,而後給ens37網卡配置IP
[root@tianqi-01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.11.136 netmask 255.255.255.0 broadcast 192.168.11.255
inet6 fe80::1eb9:8f9e:264a:7159 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:08:64:43 txqueuelen 1000 (Ethernet)
RX packets 1460 bytes 148872 (145.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1031 bytes 131035 (127.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.11.139 netmask 255.255.255.0 broadcast 192.168.11.255
ether 00:0c:29:08:64:43 txqueuelen 1000 (Ethernet)
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::8834:1ebf:d84b:7dc9 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:08:64:4d txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21 bytes 3238 (3.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 120 bytes 11061 (10.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 120 bytes 11061 (10.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@tianqi-01 ~]#
6.給ens37網卡自動獲取IP,而後再來查看ens36的網卡IP地址爲192.168.233.128
[root@tianqi-01 ~]# dhclient ens37
[root@tianqi-01 ~]#
7.這時再來使用ens36網卡的IP來訪問,會看到訪問admin目錄的狀態碼爲403
[root@tianqi-01 ~]# curl -x192.168.233.128:80 test.com/admin/
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 ~]#
8.這時再來查看日誌文件,會看到來源的IP爲192.168.233.128,在配置文件中被沒有被容許,因此爲403
[root@tianqi-01 ~]# !cat
cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:36:51 +0800] test.com "/2.jsagasg" 404 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:21:20:51 +0800] test.com "/admin/" 200 "http://www.baidu.com/1.txt" "curl/7.29.0"
192.168.11.136 - [14/Mar/2018:21:22:10 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
192.168.11.136 - [14/Mar/2018:21:23:03 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
192.168.233.128 - [14/Mar/2018:22:17:21 +0800] test.com "/admin/" 403 "-" "curl/7.29.0"
[root@tianqi-01 ~]#
[root@tianqi-01 ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/admin/
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Wed, 14 Mar 2018 14:20:30 GMT
Content-Type: text/html
Content-Length: 19
Last-Modified: Mon, 12 Mar 2018 13:43:21 GMT
Connection: keep-alive
ETag: "5aa683f9-13"
Accept-Ranges: bytes
[root@tianqi-01 ~]#
//這裏www.baidu.com是被容許的,由於來源IP是127.0.0.1
location ~ .*(abc|image)/.*\.php$ //只要匹配upload,而後以php結尾的
{
deny all; //都禁掉
}
1.打開配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.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 127.0.0.1;
allow 192.168.11.136;
deny all;
}
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
access_log /tmp/test.com.log combined_realip;
}
2.檢查配置文件語法錯誤,並從新加載配置文件
[root@tianqi-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@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
3.測試,首先是訪問的那個目錄,而後訪問的php資源
4.建立一個upload目錄,而後在建立一個php文件
[root@tianqi-01 ~]# mkdir /data/wwwroot/test.com/upload
[root@tianqi-01 ~]# echo "11111" > /data/wwwroot/test.com/upload/1.php
[root@tianqi-01 ~]#
5.訪問upload目錄下的1.php文件,會看到是403狀態碼,被拒絕訪問
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.php
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 ~]#
6.這時再upload目錄下建立1.txt,再來測試訪問
[root@tianqi-01 ~]# echo "dasdasdas" >/data/wwwroot/test.com/upload/1.txt
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.txt
dasdasdas
[root@tianqi-01 ~]#
7.查看訪問日誌cat /tmp/test.com.log
[root@tianqi-01 ~]# cat /tmp/test.com.log
127.0.0.1 - [13/Mar/2018:21:33:52 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:35:19 +0800] test.com "/index.html" 200 "-" "curl/7.29.0"
127.0.0.1 - [13/Mar/2018:21:36:51 +0800] test.com "/2.jsagasg" 404 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:21:20:51 +0800] test.com "/admin/" 200 "http://www.baidu.com/1.txt" "curl/7.29.0"
192.168.11.136 - [14/Mar/2018:21:22:10 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
192.168.11.136 - [14/Mar/2018:21:23:03 +0800] test.com "/admin/" 200 "-" "curl/7.29.0"
192.168.233.128 - [14/Mar/2018:22:17:21 +0800] test.com "/admin/" 403 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:22:20:30 +0800] test.com "/admin/" 200 "http://www.baidu.com/1.txt" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:22:30:14 +0800] test.com "/upload/1.php" 403 "-" "curl/7.29.0"
127.0.0.1 - [14/Mar/2018:22:30:49 +0800] test.com "/upload/1.txt" 200 "-" "curl/7.29.0"
[root@tianqi-01 ~]#
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
1.打開配置文件vim /usr/local/nginx/conf/vhost/test.com.conf
[root@tianqi-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.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 127.0.0.1;
allow 192.168.11.136;
deny all;
}
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
access_log /tmp/test.com.log combined_realip;
}
保存退出
2.檢查配置文件語法錯誤,並從新加載配置文件
[root@tianqi-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@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
3.模擬user_agent,訪問測試,會看到顯示403
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 06:31:01 GMT
Content-Type: text/plain
Content-Length: 10
Last-Modified: Wed, 14 Mar 2018 14:30:43 GMT
Connection: keep-alive
ETag: "5aa93213-a"
Accept-Ranges: bytes
[root@tianqi-01 ~]# curl -A "Tomatoslfdfsdf" -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 403 Forbidden
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 06:31:11 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@tianqi-01 ~]#
4.若是訪問的時候,改爲小寫再訪問,則狀態碼爲200,由於這個是嚴格匹配的
[root@tianqi-01 ~]# curl -A "tomatoslfdfsdf" -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 06:32:19 GMT
Content-Type: text/plain
Content-Length: 10
Last-Modified: Wed, 14 Mar 2018 14:30:43 GMT
Connection: keep-alive
ETag: "5aa93213-a"
Accept-Ranges: bytes
[root@tianqi-01 ~]#
5.若是想忽略大小寫,在配置文件中的匹配符號後加 * 號便可
[root@tianqi-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
6.再檢查配置文件,並從新加載
[root@tianqi-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@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
7.再來測試,會顯示403
[root@tianqi-01 ~]# curl -A "tomatoslfdfsdf" -x127.0.0.1:80 test.com/upload/1.txt -I
HTTP/1.1 403 Forbidden
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 06:35:25 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@tianqi-01 ~]#
12.15Nginx解析php相關配置
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
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;
}
1.打開虛擬主機配置文件,由於如今test.com.conf還不能解析php,加代碼添加到配置文件中
[root@tianqi-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
server_name test.com test2.com test3.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|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 127.0.0.1;
allow 192.168.11.136;
deny all;
}
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
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 combined_realip;
}
保存退出
2.生成作一個php文件,在/data/wwwroot/test.com/目錄下生成3.php
[root@tianqi-01 ~]# vim /data/wwwroot/test.com/3.php
<?php
phpinfo();
保存退出
3.測試訪問3.php,會看到沒法解析3.php文件,顯示出了源碼
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php
<?php
phpinfo();
[root@tianqi-01 ~]#
4.這時候檢查配置文件語法錯誤,並從新加載配置文件
[root@tianqi-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@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
5.這時候再來訪問3.php,會看到能夠正常解析了(會看到網頁的源碼,不少行代碼)
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php
6.如果解析php相關配置的 fastcgi_pass unix:/tmp/php-fcgi.sock; 這個路徑被寫錯,會直接顯示502,由於sock文件沒有被找到
7.將配置文件改錯後,從新加載後,再來訪問3.php,會看到顯示502狀態碼
[root@tianqi-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
//將tmp故意改爲tmpd
[root@tianqi-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@tianqi-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@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 ~]#
//這裏的狀態碼是502,由於找不到socket文件
8.查看訪問日誌cat /usr/local/nginx/logs/nginx_error.log,會看到日誌文件中會說沒有這樣的文件或目錄
[root@tianqi-01 ~]# cat /usr/local/nginx/logs/nginx_error.log
2018/03/15 15:51:23 [crit] 2135#0: *8 connect() to unix:/tmd/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "GET HTTP://test.com/3.php HTTP/1.1", upstream: "fastcgi://unix:/tmd/php-fcgi.sock:", host: "test.com"
[root@tianqi-01 ~]#
9.在遇到502的問題時,須要查看你配置的地址是否正確,首先查看錯誤日誌,而後根據錯誤日誌中提示,查看這個文件是否存在,在查看cat /usr/local/php-fpm/etc/php-fpm.conf你定義的sock是什麼,那麼在nginx的配置文件中寫什麼
[root@tianqi-01 ~]# ls /tmd/php-afcgi.sock
ls: cannot access /tmd/php-afcgi.sock: No such file or directory
[root@tianqi-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
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@tianqi-01 ~]#
10.這時再去配置文件中更改回來便可,因此只要配置文件中的 fastcgi_pass unix:/tmp/php-fcgi.sock; 地址錯誤,就會顯示502
1.假設這時不監聽sock,而去監聽IP端口
2.首先更改配置vim /usr/local/php-fpm/etc/php-fpm.conf
[root@tianqi-01 ~]# vim /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
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
保存退出
3.重啓php 命令爲/etc/init.d/php-fpm restart,php重啓也支持reload
[root@tianqi-01 ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@tianqi-01 ~]#
4.檢查php文件是否存在語法錯誤,從新加載下nginx的配置文件
[root@tianqi-01 ~]# /usr/local/php-fpm/sbin/php-fpm -t
[15-Mar-2018 16:06:52] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
[root@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
5.查看監聽端口是否爲127.0.0.1:9000
[root@tianqi-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 821/nginx: master p
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 804/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 933/master
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2188/php-fpm: maste
tcp6 0 0 :::22 :::* LISTEN 804/sshd
tcp6 0 0 ::1:25 :::* LISTEN 933/master
tcp6 0 0 :::3306 :::* LISTEN 1053/mysqld
[root@tianqi-01 ~]#
6.這時在來訪問3.php,會看到顯示爲502
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 ~]#
7.查看配置文件會提示說文件不存在
8.這時候只須要在配置文件中作一個更改,在php配置那一塊,註釋掉unix,添加ip和端口
[root@tianqi-01 ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
//在php配置那一塊,註釋掉unix,添加ip和端口
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
保存退出
9.檢查語法錯誤,並從新加載配置文件
[root@tianqi-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@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
10.再來訪問3.php文件,會看到正常訪問
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 08:49:26 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@tianqi-01 ~]#
11.如果出現502,要檢查下配置文件中的fastcgi_pass 這塊是否nginx與php-fpm中所配置的地址是相匹配的
12.配置文件中的 fastcgi_param SCRIPT_FILENAME 中的地址路徑/data/wwwroot/test.com$fastcgi_script_name;與配置文件最上方的 root /data/wwwroot/test.com; 相對應起來
1.更改php-fpm的配置文件,取消註釋listen = /tmp/php-fcgi.sock,註釋掉#listen = 127.0.0.1:9000和#listen.mode = 666
[root@tianqi-01 ~]# vim /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
#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
2.從新加載php
[root@tianqi-01 ~]# /etc/init.d/php-fpm reload
Reload service php-fpm done
[root@tianqi-01 ~]#
3.查看sock文件的權限爲660,屬主和屬組爲root
[root@tianqi-01 ~]# ls -l /tmp/php-fcgi.sock
srw-rw---- 1 root root 0 Mar 15 16:52 /tmp/php-fcgi.sock
[root@tianqi-01 ~]#
4.更改nginx虛擬主機配置文件,取消 fastcgi_pass unix:/tmp/php-fcgi.sock; 的註釋,註釋掉#fastcgi_pass 127.0.0.1:9000;
[root@tianqi-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;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
5.從新加載nginx配置文件
[root@tianqi-01 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 ~]#
6.這時候再來訪問3.php,依然仍是顯示502
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 502 Bad Gateway
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 08:55:48 GMT
Content-Type: text/html
Content-Length: 173
Connection: keep-alive
[root@tianqi-01 ~]#
7.查看訪問日誌文件,顯示訪問文件,權限被拒絕
[root@tianqi-01 ~]# tail /usr/local/nginx/logs/nginx_error.log
2018/03/15 15:51:23 [crit] 2135#0: *8 connect() to unix:/tmd/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "GET HTTP://test.com/3.php HTTP/1.1", upstream: "fastcgi://unix:/tmd/php-fcgi.sock:", host: "test.com"
2018/03/15 16:33:01 [crit] 2212#0: *10 connect() to unix:/tmp/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "GET HTTP://test.com/3.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"
2018/03/15 16:55:48 [crit] 2334#0: *14 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: test.com, request: "HEAD HTTP://test.com/3.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fcgi.sock:", host: "test.com"
[root@tianqi-01 ~]#
8.sock文件默認權限使660,root用戶能夠讀,root用戶組也是可讀的,惟獨其餘用戶不能去讀
9.看到是由nobody的身份去讀nginx的
[root@tianqi-01 ~]# ps aux |grep nginx
root 821 0.0 0.1 21280 1684 ? Ss 08:24 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/confnginx.conf
nobody 2333 0.0 0.3 23160 3448 ? S 16:55 0:00 nginx: worker process
nobody 2334 0.0 0.3 23160 3948 ? S 16:55 0:00 nginx: worker process
root 2338 0.0 0.0 112660 984 pts/0 R+ 16:57 0:00 grep --color=auto nginx
[root@tianqi-01 ~]#
10.這時臨時改變權限爲nobody
[root@tianqi-01 ~]# chown nobody /tmp/php-fcgi.sock
[root@tianqi-01 ~]#
11.這時再去訪問3.php會看到正常訪問
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 09:00:46 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@tianqi-01 ~]#
12.這就是由於nobody用戶有讀的權限,因此能夠正常訪問
13.在php-fpm的配置文件中定義listen.mode,就是爲了讓任何用戶能夠讀
14.再去配置文件中取消listen.mode的註釋
[root@tianqi-01 ~]# vim /usr/local/php-fpm/etc/php-fpm.conf
listen.mode = 666
15.而後重啓php-fpm的配置文件
[root@tianqi-01 ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@tianqi-01 ~]#
16.查看文件的權限
[root@tianqi-01 ~]# ls -l /tmp/php-fcgi.sock
srw-rw-rw- 1 root root 0 Mar 15 17:02 /tmp/php-fcgi.sock
[root@tianqi-01 ~]#
17.訪問3.php會看到正常訪問
[root@tianqi-01 ~]# curl -x127.0.0.1:80 test.com/3.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 15 Mar 2018 09:03:50 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
[root@tianqi-01 ~]#
12.16Nginx代理
• 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;
}
}
1.首先切換目錄cd /usr/local/nginx/conf/vhost
[root@tianqi-01 ~]# cd /usr/local/nginx/conf/vhost
[root@tianqi-01 vhost]#
2.新建一個配置文件vim proxy.conf
//加入如下內容
server
{
listen 80;
server_name ask.apelearn.com; //定義域名,論壇的網站
location /
{
proxy_pass http://121.201.9.155/; //定義域名,論壇的IP
proxy_set_header Host $host; //定義訪問的域名爲$host =server_name ask.apelearn.com
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
保存退出
3.配置文件中,沒有了root,由於這是一個代理服務器,它不須要訪問本地服務器上的任何文件
4.在配置完成後,這臺虛擬機就能夠訪問ask.apelearn.com論壇了
5.檢查配置文件語法錯誤,並從新加載配置文件
[root@tianqi-01 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@tianqi-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@tianqi-01 vhost]#
6.robots是針對蜘蛛的索引的一個列表,通常網站都會有robots
[root@tianqi-01 vhost]# curl ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#
User-agent: *
Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/
Disallow: /*/ajax/[root@tianqi-01 vhost]#
//robots是針對蜘蛛的索引列表,通常網站都會有這個東西
7.測試代理是否成功,指定本機的IP,也能去訪問
[root@tianqi-01 vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
[root@tianqi-01 vhost]#
8.正常狀況下,不去配置這個代理,是不可能經過本地訪問到遠程的站點的
9.這裏代理服務器就是咱們的虛擬機,WEB服務器就是論壇
友情連接:阿銘linux