12.13 Nginx防盜鏈
12.14 Nginx訪問控制
12.15 Nginx解析php相關配置
12.16 Nginx代理
擴展
502問題彙總 http://ask.apelearn.com/question/9109
location優先級 http://blog.lishiming.net/?p=100php
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 ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #{ # expires 7d; # access_log off; #}
添加一些配置css
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; }
最後結果以下html
[root@yong-01 vhost]# vim test.com.conf server { listen 80; server_name test.com test1.com test2.com; 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; }
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/1.gif -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 12:20:35 GMT Content-Type: image/gif Content-Length: 10 Last-Modified: Sat, 09 Jun 2018 01:44:01 GMT Connection: keep-alive ETag: "5b1b30e1-a" Expires: Mon, 18 Jun 2018 12:20:35 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes
[root@yong-01 vhost]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 test.com/1.gif -I HTTP/1.1 403 Forbidden Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 12:21:59 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive [root@yong-01 vhost]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 test.com/1.gif -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 12:22:11 GMT Content-Type: image/gif Content-Length: 10 Last-Modified: Sat, 09 Jun 2018 01:44:01 GMT Connection: keep-alive ETag: "5b1b30e1-a" Expires: Mon, 18 Jun 2018 12:22:11 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes
location /admin/ { allow 192.168.180.134; allow 127.0.0.1; deny all; }
location ~ .*(abc|image)/.*\.php$ { deny all; }
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; }
location /admin/ { allow 192.168.180.134; //白名單 allow 127.0.0.1; //白名單 deny all; //所有deny }
最後結果以下mysql
[root@yong-01 vhost]# vim 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.180.134; allow 127.0.0.1; deny all; } access_log /tmp/test.com.log combined_realip; }
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 test.com/admin/ -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 12:48:05 GMT Content-Type: text/html Content-Length: 19 Last-Modified: Thu, 07 Jun 2018 14:25:33 GMT Connection: keep-alive ETag: "5b19405d-13" Accept-Ranges: bytes [root@yong-01 vhost]# curl -e "http://www.test.com/1.txt" -x192.168.180.134:80 test.com/admin/ -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 12:49:43 GMT Content-Type: text/html Content-Length: 19 Last-Modified: Thu, 07 Jun 2018 14:25:33 GMT Connection: keep-alive ETag: "5b19405d-13" Accept-Ranges: bytes
[root@yong-01 vhost]# cat /tmp/test.com.log 127.0.0.1 - [09/Jun/2018:09:45:28 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [11/Jun/2018:20:48:01 +0800] test.com "/admin/" 200 "http://www.test.com/1.txt" "curl/7.29.0" 127.0.0.1 - [11/Jun/2018:20:48:05 +0800] test.com "/admin/" 200 "http://www.test.com/1.txt" "curl/7.29.0" 192.168.180.134 - [11/Jun/2018:20:49:43 +0800] test.com "/admin/" 200 "http://www.test.com/1.txt" "curl/7.29.0"
[root@yong-01 vhost]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.180.134 netmask 255.255.255.0 broadcast 192.168.180.255 inet6 fe80::8004:45b5:96c5:3ca5 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:29:2b:60 txqueuelen 1000 (Ethernet) RX packets 2816 bytes 250885 (245.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1664 bytes 221897 (216.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.100.255 inet6 fe80::af5:df02:5a53:e408 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:29:2b:6a txqueuelen 1000 (Ethernet) RX packets 153 bytes 52046 (50.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 37 bytes 3246 (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 200 bytes 17668 (17.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 200 bytes 17668 (17.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@yong-01 vhost]# curl -x192.168.100.1:80 test.com/admin/ -I HTTP/1.1 403 Forbidden Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 12:58:12 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
[root@yong-01 vhost]# !cat cat /tmp/test.com.log 127.0.0.1 - [09/Jun/2018:09:45:28 +0800] test.com "/index.html" 200 "-" "curl/7.29.0" 127.0.0.1 - [11/Jun/2018:20:48:01 +0800] test.com "/admin/" 200 "http://www.test.com/1.txt" "curl/7.29.0" 127.0.0.1 - [11/Jun/2018:20:48:05 +0800] test.com "/admin/" 200 "http://www.test.com/1.txt" "curl/7.29.0" 192.168.100.1 - [11/Jun/2018:20:57:48 +0800] test.com "/admin/" 403 "http://www.test.com/1.txt" "curl/7.29.0" 192.168.100.1 - [11/Jun/2018:20:58:12 +0800] test.com "/admin/" 403 "-" "curl/7.29.0"
location ~ .*(upload|image)/.*\.php$ //只要匹配upload,而後以php結尾的 { deny all; //都禁掉 }
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# mkdir /data/wwwroot/test.com/upload [root@yong-01 vhost]# echo "123456" > /data/wwwroot/test.com/upload/1.php
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/upload/1.php -I HTTP/1.1 403 Forbidden Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 13:08:26 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
[root@yong-01 vhost]# echo "123456" > /data/wwwroot/test.com/upload/1.txt [root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/upload/1.txt 123456
[root@yong-01 vhost]# tail -5 /tmp/test.com.log 127.0.0.1 - [11/Jun/2018:21:08:21 +0800] test.com "/upload/1.php" 403 "-" "curl/7.29.0" 127.0.0.1 - [11/Jun/2018:21:08:26 +0800] test.com "/upload/1.php" 403 "-" "curl/7.29.0" 127.0.0.1 - [11/Jun/2018:21:08:52 +0800] test.com "/upload/1.txt" 200 "-" "curl/7.29.0" 127.0.0.1 - [11/Jun/2018:21:08:57 +0800] test.com "/upload/1.txt" 200 "-" "curl/7.29.0"
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') { return 403; }
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/admin/1.txt -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 13:16:38 GMT Content-Type: text/plain Content-Length: 5 Last-Modified: Mon, 11 Jun 2018 13:16:35 GMT Connection: keep-alive ETag: "5b1e7633-5" Accept-Ranges: bytes [root@yong-01 vhost]# curl -A "Tomatosfdas" -x127.0.0.1:80 test.com/admin/1.txt -I HTTP/1.1 403 Forbidden Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 13:17:10 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
[root@yong-01 vhost]# curl -A "tomatosfdas" -x127.0.0.1:80 test.com/admin/1.txt -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 13:17:38 GMT Content-Type: text/plain Content-Length: 5 Last-Modified: Mon, 11 Jun 2018 13:16:35 GMT Connection: keep-alive ETag: "5b1e7633-5" Accept-Ranges: bytes
[root@yong-01 vhost]# vim test.com.conf if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato') { return 403; }
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -A "tomatosfdas" -x127.0.0.1:80 test.com/admin/1.txt -I HTTP/1.1 403 Forbidden Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 13:19:29 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
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; }
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; //寫錯這個路徑,就會顯示502 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; }
[root@yong-01 vhost]# vim /data/wwwroot/test.com/3.php <?php phpinfo();
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/3.php <?php phpinfo();
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/3.php
[root@yong-01 vhost]# 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.4.7</center> </body> </html>
[root@yong-01 vhost]# cat /usr/local/nginx/logs/nginx_error.log 2018/06/11 21:44:48 [crit] 2348#0: *33 connect() to unix:/tmp/php-cgi.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-cgi.sock:", host: "test.com"
這兩個地方的路徑必須是同樣的 不能錯 不然就報502錯誤nginx
[root@yong-01 vhost]# 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
[root@yong-01 vhost]# service php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
[root@yong-01 vhost]# /usr/local/php-fpm/sbin/php-fpm -t [11-Jun-2018 21:51:56] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# 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 1178/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1120/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1477/master tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 2497/php-fpm: maste tcp6 0 0 :::22 :::* LISTEN 1120/sshd tcp6 0 0 ::1:25 :::* LISTEN 1477/master tcp6 0 0 :::3306 :::* LISTEN 1431/mysqld
[root@yong-01 vhost]# 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.4.7</center> </body> </html>
[root@yong-01 vhost]# vim test.com.conf 在php配置那一塊,註釋掉unix,添加ip和端口 #fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_pass 127.0.0.1:9000; 保存退出
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/3.php
[root@yong-01 vhost]# 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
[root@yong-01 vhost]# /etc/init.d/php-fpm reload Reload service php-fpm done
[root@yong-01 vhost]# ll /tmp/php-fcgi.sock srw-rw---- 1 root root 0 6月 11 21:57 /tmp/php-fcgi.sock
[root@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-01 vhost]# 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.4.7</center> </body> </html>
[root@yong-01 vhost]# tail /usr/local/nginx/logs/nginx_error.log 2018/06/11 21:44:48 [crit] 2348#0: *33 connect() to unix:/tmp/php-cgi.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-cgi.sock:", host: "test.com" 2018/06/11 21:53:20 [crit] 2523#0: *35 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/06/11 21:59:12 [crit] 2650#0: *39 connect() to unix:/tmp/php-fcgi.sock failed (13: Permission denied) 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"
[root@yong-01 vhost]# ps aux |grep nginx root 1178 0.0 0.0 25584 1824 ? Ss 19:51 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 2649 0.0 0.1 27276 3600 ? S 21:58 0:00 nginx: worker process nobody 2650 0.0 0.2 27276 3848 ? S 21:58 0:00 nginx: worker process root 2672 0.0 0.0 112676 984 pts/0 R+ 22:00 0:00 grep --color=auto nginx
[root@yong-01 vhost]# chown nobody /tmp/php-fcgi.sock
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/3.php
[root@yong-01 vhost]# vim /usr/local/php-fpm/etc/php-fpm.conf listen.mode = 666
[root@yong-01 vhost]# service php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
[root@yong-01 vhost]# ll /tmp/php-fcgi.sock srw-rw-rw- 1 root root 0 6月 11 22:02 /tmp/php-fcgi.sock
[root@yong-01 vhost]# curl -x127.0.0.1:80 test.com/3.php -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Mon, 11 Jun 2018 14:03:18 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30
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; } }
[root@yong-01 vhost]# 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; } }
[root@yong-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@yong-01 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@yong-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@yong-01 vhost]# curl -x127.0.0.1:80 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/