nginx访问控制、rewrite应用、代理设置
一、访问控制
在这里依然还是以default2.conf虚拟主机为例,配置文件位置default2.conf
1、允许某个ip访问 ,需要在default2.conf配置配文件中添加,具体如下图:
规则如下:
allow 127.0.0.1;
allow 192.168.21.97;
deny all;
只允许127.0.0.1和192.168.21.97来访问,其他的全部拒绝
退出保存
1)检查配置文件
2)重置配置文件
3)测试
允许ip测试
[root@mysql ~]# curl -x192.168.21.97:80 http://www.guhantai.cn/1.jpg-I HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Mon, 22 Jun 2015 15:21:05 GMT Content-Type: image/jpeg Content-Length: 0 Last-Modified: Mon, 22 Jun 2015 01:16:22 GMT Connection: keep-alive ETag: "558761e6-0" Expires: Thu, 02 Jul 2015 15:21:05 GMT Cache-Control: max-age=864000 Accept-Ranges: bytes
其他ip测试
2、 禁止某个IP访问
禁止本地IP地址访问
规则如下:
deny 127.0.0.1;
allow all;
1)保存退出
2)检查配置文件
/usr/local/nginx/sbin/nginx -t
3)重置配置文件
[root@mysql ~]# /usr/local/nginx/sbin/nginx -s reload
4)测试
使用127.0.0.1来个访问guhantai.cn
[root@mysql ~]# curl -x127.0.0.1:80 http://www.guhantai.cn/1.jpg <html> <head><title>403 Forbidden</title></head> #提示被禁止 <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.6.2</center> </body> </html>
使用192.168.21.97测试
[root@mysql ~]# curl -x192.168.21.97:80 http://www.guhantai.cn/1.jpg-I HTTP/1.1 200 OK #访问是OK的 Server: nginx/1.6.2 Date: Mon, 22 Jun 2015 14:52:58 GMT Content-Type: image/jpeg Content-Length: 0 Last-Modified: Mon, 22 Jun 2015 01:16:22 GMT Connection: keep-alive ETag: "558761e6-0" Expires: Thu, 02 Jul 2015 14:52:58 GMT Cache-Control: max-age=864000 Accept-Ranges: bytes
3、某个目录下限制IP访问,这个的作用主要就是是针对一些特定的目录来限制访问,依然以default2.conf虚拟主机配置为例
只允许192.168.61.0/24这个网段对w目录访问,其他的全部拒绝,配置文件位置:/usr/local/nginx/conf/vhosts/default2.conf
如图:
规则写法如下:
location /w/ {
allow 192.168.61.0/24; #这里的IP段就是允许的IP段
deny all;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
一定要加上location php,不然是不会去解析php的
退出保存
检查配置文件
[root@mysql w]#
/usr/local/nginx/sbin/nginx -t
重启nginx
/etc/init.d/nginx restart
测试,我自己的win7 电脑ip是21网段的,所以无法访问
4、在nginx的配置文件nginx.conf中加入:
include deny.ip;
重启一下nginx的服务:/usr/local/nginx/sbin/nginx reload就可以生效了。
deny.ip 的格式中也可以用deny all;
如果你想实现这样的应用,除了几个IP外,其他全部拒绝,
那需要你在deny.ip 中这样写
allow 1.1.1.1;
allow 1.1.1.2;
deny all;
5、有时候会根据目录来限制php解析,这里依然还是以/usr/local/nginx/conf/vhosts/default2.conf 虚拟主机为例的
location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.php$ #括号里面的表示或者的意思,只要是括号里面的,不管哪一个都禁止
{
deny all;
}
这个的意思就是说访问这些目录后的php全部不解析
添加在这个位置,如下图:
退出保存
检查配置文件
/usr/local/nginx/sbin/nginx -t
重启nginx
/etc/init.d/nginx restart
测试访问www.guhantai.cn 后面带image目录下后缀为.php的文件时,提示拒绝访问
[root@mysql ~]# curl -x192.168.21.97:80 http://www.guhantai.cn/wer/image/23.php
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
6、使用 user_agent 控制客户端访问 ,就是可以限制某些浏览器来访问,只需要将浏览器的标示写入到规则里面就可以,都是在nginx虚拟主机配置文件中设置的
代码如下:
if ($http_user_agent ~ ‘bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315‘){
return 403;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
二、nginx的rewrite应用
1、rewrite作用
重写URL,或者修改字符串。需要注意的是重写URL只对相对路径有效,如果想要对主机名,要使用if语句
2、伪静态rewrite规则,一下这规则可以作为一个标准的模板,需要的时候直接复制粘贴就可以,当然这个也是写在虚nginx虚拟主机的配置文件中的
伪静态rewrite规则
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
参考文档 :
Rewrite设置及示例 http://www.lishiming.net/thread-239-1-1.html
nginx $document_uri 参数使用 http://www.lishiming.net/thread-993-1-1.html
nginx的301与302如何配置 http://www.lishiming.net/thread-4840-1-1.html
nginx rewrite不支持if 嵌套也不支持逻辑或和逻辑并 http://www.lishiming.net/thread-4842-1-1.html
三、 nginx 代理
一般在真实生产环境中,主要是代理自己的网站,而不是别人的网站。
1、在/usr/local/nginx/conf/vhosts目录下写一个proxy.conf文件
写入以下代码:
server {
listen 80;
server_name www.baidu.com; #这里的域名就是所要代理的域名
location / {
proxy_pass http://115.239.210.27/; #这里的IP地址一定要是代理域名的真实IP地址才可以的
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# access_log /home/logs/aaa_access.log combined;
}
退出保存
检查配置文件
重置nginx服务
测试
没有配置之前:
[root@mysql ~]# curl -x 192.168.21.97:80 www.baidu.com <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
配置代理之后:
[root@mysql vhosts]#curl -x 192.168.21.97:80 www.baidu.com <!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /><link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu.svg"><link rel="dns-prefetch" href="//s1.bdstatic.com"/><link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/><link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/><link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/><link rel="dns-prefetch" href="//b1.bdstatic.com"/><title>百度一下,你就知道</title>
………...内容太多了,就不写了
2、如果后端有多台机器,使用如下代码来写。
代码如下:
upstream bbb #这里是设置这两个ip的别名
{
server 1.2.3.1:80;
server 1.2.3.4:80;
}
server {
listen 80;
server_name bbb.com;
location / {
proxy_pass http://bbb/; #这里的bb是别名
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# access_log /home/logs/bb_access.log combined;
}
3、代理一个服务器上所有域名
首先在vhosts目录下需要建立两个文件,一个是servername 列表文件,一个是虚拟主机配置文件
两个文件内容分别为
(1) servername
server_name www.123.net.cn www.alsdjfl.com www.asdfa1.com; //就这么简单一行,当然这个server_name 还可以继续添加的
(2) 虚拟主机配置文件
server {
listen 80;
include vhosts/servername; // 这里的文件就是上边那个servername列表文件
location / {
proxy_pass http://1.2.1.2/; //这里就是需要做代理的服务器ip地址了
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log /dev/null;
}
参考文档:
根据访问的目录来区分后端的web http://www.lishiming.net/thread-920-1-1.html
针对请求的uri来代理 http://www.lishiming.net/thread-1049-1-1.html
实验配置代码参考:
server { listen 80; server_name www.1.com www.a.com www.b.com; #域名跳转 if ($host != ‘www.a.com‘ ) { rewrite ^/(.*)$ http://www.a.com/$1 permanent; } index index.html index.htm index.php; root /data/www; # location /uc_server/ { # auth_basic "Auth"; # auth_basic_user_file /usr/local/nginx/conf/.htpasswd; # } #黑名单 # deny 127.0.0.1; # allow all; #白名单 # allow 127.0.0.1; # allow 192.168.31.141; # deny all; #某个目录下限制ip location /uc_server/ { allow 192.168.31.0/24; deny all; location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; } } #针对目录限制php解析 location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.php$ { deny all; } #根据user_agent控制 if ($http_user_agent ~ ‘bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315‘){ return 403; } location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; } #缓存时间 # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ # { # expires 30d; # access_log off; # } location ~ .*\.(js|css)?$ { expires 12h; access_log off; } #防盗链 location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$ { expires 10d; valid_referers none blocked server_names *.1.com *.a.com *.b.com *.baidu.com *.google.com *.google.cn *.soso.com ; if ($invalid_referer) { return 403; #rewrite ^/ http://www.example.com/nophoto.gif; } access_log off; } # 伪静态rewrite规则 rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last; rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last; rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last; rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last; rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last; rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last; #docment_uri # if ($document_uri !~ ‘abc‘) # { # rewrite ^/(.*)$ /abc/$1 redirect; # } access_log /home/logs/discuz.log combined_realip; }
笔记有错误的地方还请大神指正,小白会继续修改
本文出自 “IT交流学习” 博客,请务必保留此出处http://sxct168.blog.51cto.com/824373/1668685
原文地址:http://sxct168.blog.51cto.com/824373/1668685