本文介绍了替代else运算符和不等于运算符在nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,在cookie的基础上,我必须转发请求到某个apache端口。我想要这样的东西:
In my application, on the basis of cookie, I have to forward the request to certain apache port. I want something like this:
server {
listen 80;
server_name example.com;
location /
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-FORWARDED_PROTO https;
if ($http_cookie ~ 'ver=1' ) {
proxy_pass http://127.0.0.1:6060;
}
else {
proxy_pass http://127.0.0.1:7070;
}
}
}
!〜不允许在nginx.conf中,我可以做什么类型的需求?
As "else" and "!~" are not allowed in nginx.conf, what can I done for such type of requirement?
推荐答案
使用像这样:
server {
listen 80;
server_name ver.jeevansathi.com;
location /
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-FORWARDED_PROTO https;
set $cookie_redirect 0;
if ($http_cookie ~ 'ver=1' ) {
set $cookie_redirect 1;
}
if ($cookie_redirect ~ 1) {
proxy_pass http://127.0.0.1:6060;
}
if ($cookie_redirect ~ 0 ) {
proxy_pass http://127.0.0.1:7070;
}
}
}
这篇关于替代else运算符和不等于运算符在nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!