0
点赞
收藏
分享

微信扫一扫

nginx反向代理,实现地址栏域名不变,session不失效


首先找到nginx.conf部分

server {
listen 80;
server_name www.taekwondo-china.com;
location / { #所有以/开头的地址,实际上就是所有请求
proxy_pass http://event.geexek.com/7918/;
}
location ^~ /7918 {
rewrite ^/(.*) /$1 break;
proxy_pass http://event.geexek.com;
}

location ~ .*\.(js|css|gif|jpg|png|ico|jpeg)$ {
rewrite ^/(.*) /$1 break;
proxy_pass http://event.geexek.com;
}

location ^~ /errorpage {
root /home/nginx/nginx_data;
}

error_page 500 502 503 504 /errorpage/500.html;
error_page 400 404 /errorpage/404.html;
}

location / { #所有以/开头的地址,实际上就是所有请求
proxy_pass http://event.geexek.com/7918/;
}

在浏览器的地址栏里面输入:www.taekwondo-china.com,将请求转发到​​http://event.geexek.com/7918/​​​,得到响应返回www.taekwondo-china.com,显示来自​​http://event.geexek.com/7918/​​,但是地址栏里面的地址还是www.taekwondo-china.com。

location ^~ /7918 {
rewrite ^/(.*) /$1 break;
proxy_pass http://event.geexek.com;
}

location ^~ /7918

用于拦截请求,示例:​​http://event.geexek.com/7918/​​ 匹配以/7918开头的地址,匹配符合以后,停止往下搜索正则。

rewrite ^/(.*) /$1 break;

重写拦截进来的请求,并且只能对域名后边的除去传递的参数外的字符串起作用。例如:​​http://www.taekwondo-china.com/7918/content/13446​​​ 会对/content/13446
重写。实际访问的地址为:​​​http://event.geexek.com/7918/content/13446​​

rewrite后面的参数是一个简单的正则 ^/(.*) /,:匹配字符串的开始:匹配字符串的结束
(.).代表任何字符 代表所有的,也就是(.*)选取了/7918/之后的所有字符并存入变量1.1是重写后的表达式。

最后的参数共有四个取值:
last 处理完请求后,跳出location,再重新执行。
break 处理完成之后,不会跳出location,不再重新执行。而只能将信息传递给后面的fsatcgi_pass或者proxy_pass等指令。
permanent 永久性重定向,请求日志中的状态码为301
redirect 临时重定向,请求日志中的状态码为302

proxy_pass ​​http://event.geexek.com​​;

把请求代理到其他的主机,其中​​http://event.geexek.com/​​​写法和​​http://event.geexek.com​​写法的区别:

​​http://event.geexek.com​​​写法:访问路径会变成​​http://event.geexek.com/7918/XXX​​​
​​​http://event.geexek.com/​​​写法:访问路径会变成​​http://event.geexek.com/XXX​​

BUT 如果你的系统使用了session,你会发现自己登录不上了。

所以要改一种配置方式

首先要让session生效

#skip.geexek.cn
upstream SKIP {
server 10.XX.XXX.181:9704;
}

server {
listen 80;
server_name skip.geexek.cn;

location / {
proxy_next_upstream error timeout http_500 http_502 http_504;
proxy_read_timeout 60s;
proxy_set_header Host $host;
proxy_pass http://SKIP;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors on;
client_max_body_size 15M;
index index.html index.htm;
rewrite ^/$ /7235 last;
}
location ^~ /errorpage {
root /home/nginx/nginx_data;
}

error_page 500 502 503 504 /errorpage/500.html;
error_page 400 404 /errorpage/404.html;
}

我们给10.XX.XXX.181:9704的这个工程反向代理为需要地址栏中需要显示的地址
skip.geexek.cn,配置好之后你使用​​​http://skip.geexek.cn/7235​​​ 可以完成用户的登录,但是 ​​http://skip.geexek.cn​​ 无法找到自己的对应的网站。

怎样才能既保持session又能使用​​http://skip.geexek.cn​​​ 访问到​​http://skip.geexek.cn/7235​​呢?需要我们再最后加上

rewrite ^/$  http://skip.geexek.cn/7235 break;

这样之后,就能保证session不失效,而且​​http://skip.geexek.cn​​​能自动跳转到​​http://skip.geexek.cn/7235​​。

url重写是指通过配置conf文件,以让网站的url中达到某种状态时则定向/跳转到某个规则,比如常见的伪静态、301重定向、浏览器定向等

rewrite

语法:

在配置文件的server块中写,如:

server {
rewrite 规则 定向路径 重写类型;
}

规则:

可以是字符串或者正则来表示想匹配的目标url

定向路径:

表示匹配到规则后要定向的路径,如果规则里有正则,则可以使用$index来表示正则里的捕获分组

重写类型:
last :相当于Apache里的(L)标记,表示完成rewrite,浏览器地址栏URL地址不变

break;本条规则匹配完成后,终止匹配,不再匹配后面的规则,浏览器地址栏URL地址不变

redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址

permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
简单例子

server {
# 访问 /last.html 的时候,页面内容重写到 /index.html 中
rewrite /last.html /index.html last;
# 访问 /break.html 的时候,页面内容重写到 /index.html 中,并停止后续的匹配
rewrite /break.html /index.html break;
# 访问 /redirect.html 的时候,页面直接302定向到 /index.html中
rewrite /redirect.html /index.html redirect;
# 访问 /permanent.html 的时候,页面直接301定向到 /index.html中
rewrite /permanent.html /index.html permanent;
# 把 /html/*.html => /post/*.html ,301定向
rewrite ^/html/(.+?).html$ /post/$1.html permanent;
# 把 /search/key => /search.html?keyword=key
rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent;
}

last和break的区别

因为301和302不能简单的只返回状态码,还必须有重定向的URL,这就是return指令无法返回301,302的原因了。这里 last 和 break 区别有点难以理解:

last一般写在server和if中,而break一般使用在location中

last不终止重写后的url匹配,即新的url会再从server走一遍匹配流程,而break终止重写后的匹配

break和last都能组织继续执行后面的rewrite指令

在location里一旦返回break则直接生效并停止后续的匹配location

server {
location / {
rewrite /last/ /q.html last;
rewrite /break/ /q.html break;
}
location = /q.html {
return 400;
}
}

访问/last/时重写到/q.html,然后使用新的uri再匹配,正好匹配到locatoin = /q.html然后返回了400

访问/break时重写到/q.html,由于返回了break,则直接停止了


举报

相关推荐

0 条评论