LNMP(nginx防盜鏈,訪問控制,解析php相關配置,Nginx代理,常見502問題)
nginx防盜鏈:
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 添加以下內容
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; 如果不是,則返回403錯誤碼
}
access_log off;
}
檢測語法錯誤並且重新加載配置文件:
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
測試:(百度跳過來的顯示403,test正常返回值)
[root@lnmp ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 test.com/1.gif
<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>
[root@lnmp ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 test.com/1.gif
dasdasdafasdfaf
二、Nginx訪問控制
nginx防盜鏈:
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf 添加以下內容
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; 如果不是,則返回403錯誤碼
}
access_log off;
}
檢測語法錯誤並且重新加載配置文件:
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
測試:(百度跳過來的顯示403,test正常返回值)
[root@lnmp ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 test.com/1.gif
<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>
[root@lnmp ~]# curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 test.com/1.gif
dasdasdafasdfaf
三、Nginx解析php相關配置
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/test.com.conf (配置虛擬主機配置文件)
cation ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; (用來監聽php-fpm的地址或者socket,這裏怎麽寫取決於/usr/local/php-fpm/etc/php-fpm.conf裏的listen怎麽寫,如果不一樣,則curl會報502錯誤,)
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
這裏可以對比一下php-ftm的配置文件。
[root@lnmp ~]# 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 (如果這裏寫成端口,則虛擬配置文件裏也要寫成:fastcgi_pass 127.0.0.1:9000)
listen.mode = 666 (定義php-fcgi.sock的權限,必須是666,否則nginx解析不了)
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@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
檢測:
[root@lnmp ~]# curl -x127.0.0.1:80 test.com/1.php
出現的就是phpinfo()函數的界面
四、Nginx代理
場景:如果要從國內訪問美國的服務器會很慢,這時候就可以找個香港服務器做代理,香港訪問美國是很快的。
代理服務器作為用戶和web服務器的代理者。
配置:(因為是代理服務器,不用訪問本機的配置文件)
[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/proxy.conf
server
{
listen 80;
server_name ask.apelearn.com; 定義域名
location /
{
proxy_pass http://121.201.9.155/; 告訴nginx真正的web服務器地址
proxy_set_header Host $host; (訪問的域名是server_name)
proxy_set_header X-Real-IP $remote_addr; (定義公網ip)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; (定義代理服務器ip)
}
}
檢查語法錯誤並且重新加載配置文件
[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
檢查:(從本機訪問到了遠程站點,說明代理成功)
[root@lnmp ~]# 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/
五、常見502的問題
1.配置錯誤
因為nginx找不到php-fpm了,所以報錯,一般是fastcgi_pass後面的路徑配置錯誤了,後面可以是socket或者是ip:port
2.資源耗盡
lnmp架構在處理php時,nginx直接調取後端的php-fpm服務,如果nginx的請求量偏高,我們又沒有給php-fpm配置足夠的子進程,那麽php-fpm就會資源耗盡,一旦資源耗盡nginx找不到php-fpm就會出現502錯誤,
解決方案
去調整php-fpm.conf中的pm.max_children數值,使其增加,但是也不能無限增加,畢竟資源有限,一般4G內存機器如果跑php-fpm和nginx,不跑mysql可以設置為150,8G為300以此類推
3.除了上面的兩種錯誤還有其他的原因,很少有,我們可以借助nginx的錯誤日誌來進行排查vim /usr/local/nginx/logs/nginx_error.log 我們也可以給日誌定義級別vim/usr/local/nginx/conf/nginx.conf 找到error_log,默認是crit最嚴謹的就行,也可以改成debug顯示的信息最全面,但是很容易撐爆我們的磁盤。
首先我們需要讓瀏覽器進行訪問
修改nginx的配置文件
[root@wqslinux ~]# vim/usr/local/nginx/conf/vhosts/111.conf
server
{
listen 80;
server_name www.111.com; //域名地址
index index.html index.htm index.php;
root /data/www/;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock; //修改sock
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
檢查語法是否正常
[root@wqslinux ~]#/usr/local/nginx/sbin/nginx -t
重新加載配置文件
[root@wqslinux ~]# /usr/local/nginx/sbin/nginx-s reload
[root@wqslinux ~]# /etc/init.d/nginx reload
檢查nginx是那個用戶跑的
[root@wqslinux ~]# ps aux |grep nginx
編輯php-fpm文件
我們要在這個php-fpm文件裏面設置nginx的用戶主,跟組這樣才不會顯示502
[root@wqslinux ~]# vim/usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log =/usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/www.sock
user = php-fpm
group = php-fpm
listen.owner = nobody //定義屬主
listen.group = nobody //定義屬組
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
配置完之後重啟php-fpm
[root@wqslinux ~]# /etc/init.d/php-fpm restart
ps: 再補充一個,是近期很多人遇到的問題
這種情況下,使用的是socket,版本高於5.4(含5.4) 默認監聽的socket文件權限是所有者只讀,屬組和其他用戶沒有任何權限。所以,nginx的啟動用戶(咱們配置的是nobody)就沒有辦法去讀這個socket文件,最終導致502,這個問題可以在nginx的錯誤日誌中發現。解決辦法很簡單,上面給出的配置文件中就有避免這個問題的配置。
listen.owner = nobody //定義屬主
listen.group = nobody //定義屬組
這兩個配置就是定義socket的屬主和屬組是誰。除了這個還有一種方法
listen.mode = 777
這樣nobody也可以有讀取權限了。
LNMP(nginx防盜鏈,訪問控制,解析php相關配置,Nginx代理,常見502問題)