nginx根据条件跳转+跳转规则

好的参考博文:

nginx rewrite规则

自己写的时候参考这两个

Nginx 伪静态Rewrite,重定向Location配置总结(转)  

 nginx rewrite规则语法

 

一、输入子目录跳转

location /templets {
        proxy_pass http://testServer/templets/;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

      location / {
        proxy_pass http://myServer;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }

当nginx主机输入 $host/templets 时会跳转进入

bb.bb.bb.bb:80;(可为其他web应用服务器ip)
upstream myServer {
      #weigth参数表示权值,权值越高被分配到的几率越大
      #本机上的Squid开启3128端口
      server xx.xx.xx.xx:80;
    }
    upstream testServer {
      server bb.bb.bb.bb:80;
    }

二、根据条件跳转

 server {
      listen 80;
      server_name  www.xx.com;
      server_name  www.xx.com.cn;
      server_name  www.xx.net;
      server_name  www.xx.net.cn;
if ( $host != 'www.xx.com' ) #如果用户输入的不是 https://www.xx.com 则转换成 https://www.xx.com/后缀 保证永远是 com 其他域名跳转到 .com ssl只需要申请一个就行 { rewrite ^(.*)$ https://www.xx.com$1 permanent; } location ~ ^/NginxStatus/ { stub_status on; access_log off; } rewrite ^(.*)$ https://$host$1 permanent; # http 强制转 https 这个不可少少了有时会报错无法转换成 https 和上面的转换缺一不可
..............
..............
}

 三、公司要求 news 二级目录跳转到另外一个 主机  但是 资源放在 当前主机二级目录 templets 下,而且当前主机配置ssl  所有http需要转成 https  ,而news 的目标主机是 http

配置如下:

 
  

#设定负载均衡的服务器列表
upstream myServer {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的apache开8080端口
server 127.0.0.1:8080;
# ip_hash;
}
upstream 3CMSServer {
server 目标ip:80;
}


server { listen
80; server_name www.xxx.com; server_name www.xxx.com.cn; server_name www.xxx.net; server_name www.xxx.net.cn;if ( $host != 'www.xxx.com' ) { rewrite ^(.*)$ https://www.xxx.com$1 permanent; #强制域名 } if ( $request_uri !~* '/news|/templets' ) { rewrite ^(.*)$ https://$host$1 permanent; # http 强制转 https } location ~ ^/NginxStatus/ { stub_status on; access_log off; } rewrite https://www.xxx.com/shouce.html https://www.xxx.com/ permanent; # 这两个写的有错 强制转 http://www.xxx.com/shouce.html 到主页 rewrite https://www.xxx.net.cn/ https://www.xxx.com/ permanent; location / { proxy_pass http://myServer/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 50m; client_body_buffer_size 512k; proxy_connect_timeout 1024; proxy_send_timeout 960; proxy_read_timeout 900; proxy_buffer_size 128k; proxy_buffers 32 128k; proxy_busy_buffers_size 512k; } location /news { proxy_pass http://3CMSServer/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /templets { proxy_pass http://3CMSServer/templets/; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

 

你可能感兴趣的:(nginx根据条件跳转+跳转规则)