nginx域名跳转技巧

1.地址重写:访问server_name的时候跳转到http://www.cnblogs.com/qinyujie/

修改nginx配置文件。加入到server{...}字段或者location字段里面:使用rewrite301跳转到指定的地址。

listen 80;

server_name www.qinyujie.com;

rewrite ^/(.*)$ http://www.cnblogs.com/qinyujie/$1 last;

2.企业安全,nginx禁止使用ip访问:

修改nginx配置文件:

http:{...}中加入如下字段:

server {

listen 80 default;

return 500;

}

3.nginx配置反向代理

location / {

proxy_set_header Host $host; #修改发送到后端的header的host

proxy_set_header X-Real-Ip $remote_addr; #设置真实ip

proxy_set_header X-Forwarded-For $remote_addr;

proxy_pass http://192.168.0.162:80; #后端ip地址

}

4.nginx的http负载均衡模块:在http:{...}字段中配置;'

upstream linuxidc { #定义负载均衡设备的Ip及设备状态

 ip_hash; #session会话保持

server xxxx:7080 down;

server xxxx:8980 weight=7;

  server xxxx:9999 backup;

}

在location / {...}中加入“‘ proxy_pass http://linuxidc; ’”即可。

ip_hash(访问ip)

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

upstream还可以为每个设备设置状态值,这些状态值的含义分别如下:

down 表示单前的server暂时不参与负载.

weight 默认为1.weight越大,负载的权重就越大。

max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.

fail_timeout : max_fails次失败后,暂停的时间。

backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

5.官网一级域名跳转(301重定向)

需求:在IE浏览器下打开9wee.com直接跳转到www.qinyj.top,方便旗下游戏内容获取

修改nginx配置文件在,root后添加

if ($host = "qinyj.top")

{

rewrite ^(.*) http://www.qinyj.top$1 permanent;

    # return 403; #可根据远程ip拒绝某个频繁访问的ip,

}

if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot")

{

return 403; # if条件语句可以控制url的跳转。可防治爬虫。防搜索引擎的

}

重启/etc/init.d/nginx reload 即可

6.nginx访问控制

nginx中内置ngx_http_access_module

加入到location 生效区域内:

  location / {

  deny 192.168.1.1;

  allow 192.168.1.0/24;

  allow 10.1.1.0/16;

  allow 2001:0db8::/32;

  deny all;

  }

重启nginx即可。