打开虚拟主机配置文件
[root@shuai-01 ~]# 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 ;#定义白名单if ($invalid_referer) { return 403;}#不是白名单的referer ,返回403access_log off;}12345678910111213
把稳:location ~ ^.+.这里匹配到的后面的内容是不区分大小写。
测查配置文件语法并重新加载:
[root@shuai-01 ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload12345
测试:
[root@shuai-01 ~]# curl -e \公众http://www.qq.com/1.txt\公众 -x127.0.0.1:80 -I test.com/1.gifHTTP/1.1 403 ForbiddenServer: nginx/1.12.2Date: Tue, 09 Jan 2018 11:00:19 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive[root@shuai-01 ~]# curl -e \"大众http://www.test.com/1.txt\"大众 -x127.0.0.1:80 -I test.com/1.gifHTTP/1.1 200 OKServer: nginx/1.12.2Date: Tue, 09 Jan 2018 11:01:15 GMTContent-Type: image/gifContent-Length: 12Last-Modified: Mon, 08 Jan 2018 16:38:47 GMTConnection: keep-aliveETag: \"大众5a539e97-c\"大众Expires: Tue, 16 Jan 2018 11:01:15 GMTCache-Control: max-age=604800Accept-Ranges: bytes123456789101112131415161718192021Nginx的访问掌握
关于做防盗链和访问掌握的缘故原由我在httpd做访问掌握和防盗链时已经说得比较清楚了。
http://blog.csdn.net/aoli_shuai/article/details/78895746
需求:访问/admin/目录,只许可那几个IP进行访问。
打开虚拟主机配置文件
location /admin/{ allow 127.0.0.1; allow 192.168.176.135; deny all;}1234567
这里Nginx设置访问掌握和Apache是有很大的不同,关于这个allow,deny。Apache是有一个顺序的,Nginx没有。
测查配置文件语法并重新加载:
[root@shuai-01 ~]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@shuai-01 ~]# /usr/local/nginx/sbin/nginx -s reload12345
检测:
[root@shuai-01 ~]# curl -e \"大众http://www.test.com/1.txt\"大众 -x127.0.0.1:80 -I test.com/admin/HTTP/1.1 200 OKServer: nginx/1.12.2Date: Tue, 09 Jan 2018 11:23:23 GMTContent-Type: text/htmlContent-Length: 5Last-Modified: Tue, 09 Jan 2018 11:23:20 GMTConnection: keep-aliveETag: \"大众5a54a628-5\"大众Accept-Ranges: bytes[root@shuai-01 ~]# curl -x192.168.176.135:80 test.com/admin/ -IHTTP/1.1 200 OKServer: nginx/1.12.2Date: Tue, 09 Jan 2018 11:25:02 GMTContent-Type: text/htmlContent-Length: 5Last-Modified: Tue, 09 Jan 2018 11:23:20 GMTConnection: keep-aliveETag: \"大众5a54a628-5\"大众Accept-Ranges: bytes12345678910111213141516171819202122
针对正则进行访问掌握:
location ~ .(abc|image)/.\.php${ deny all;}12345
针对user_agent进行限定:
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato'){ return 403;}12345
return 403 和deny all 效果是一样的。
Nginx解析PHP的干系配置核心配置:
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; # fastcgi_pass 127.0.0.1:9000; 如果php-fpm配置监听配置的是IP就写这个 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name; }123456789
Nginx要想解析PHP就要将这段核心配置写入配置文件中去。
fastcgi_pass 用来指定php-fpm监听的地址或者socket
如果是用的sock那么一定要放开php配置中的listen.mode=666(sock的权限位一定要有写的权限)
unix:/tmp/php-fcgi.sock这里的sock文件是php-fpm.conf中定义的
cat /usr/local/php-fpm/etc/php-fpm.conf配置文件中写什么就定义什么
如果php监听的是ip和端口,nginx中的配置文件就要改成
fastcgi_pass 127.0.0.1:9000;
fastcgi_param 中的路径也须要跟上面对应起来
Nginx502问题涌现及办理方法:
http://ask.apelearn.com/question/9109
Nginx代理Nginx代理分正向代理和反向代理。
http://blog.csdn.net/zjf280441589/article/details/51501408
Nginx代理是在一台代理做事器中自定义一个域名(这个域名基本上跟web做事器的域名相同),该域名指向一个IP(web做事器),然后将用户的要求通过这台代理做事器访问指定的IP所对应的web做事器。
做代理做事器要先做一个新的虚拟主机配置文件。
[root@shuai-01 ~]# cd /usr/local/nginx/conf/vhost/[root@shuai-01 vhost]# [root@shuai-01 vhost]# vim proxy.conf1234
配置文件:
server{ listen 80; server_name ask.apelearn.com;#定义代理做事器域名,一样平常的和web做事器域名相同 location / { proxy_pass http://121.201.9.155/;#指定web做事器IP proxy_set_header Host $host;#$host便是server_name proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}1234567891011121314
保存退出并检讨配置文件语法,重新加载
[root@shuai-01 vhost]# curl -x127.0.0.1:80 ask.apelear#.com/robots.txt# robots.txt for MiWen#User-agent: Disallow: /?/admin/Disallow: /?/people/Disallow: /?/question/Disallow: /account/Disallow: /app/Disallow: /cache/Disallow: /install/