防止重定向到 https 和 URI hacks nginx 反向代理
prevent redirecting to https and URI hacks nginx reverse proxy
我在我的服务器上设置了一个反向代理,里面有 nginx docker,从那时起,所有请求都重定向到 https,尽管我没有将所有位置都设置为 https 重定向。
基本上我想要的是能够使用反向代理服务 https 和 http。
此外,我希望能够动态地重定向到不同的 URI,例如,我想将 /bla2/foo/bar 的所有路由都设置为仅重定向到 /bla2 之后的内容
我试图到达这里的是,每当访问 example.com/bla2/foo/bar 时,它应该将其重定向到 example.com/foo/bar 没有 bla2 部分...
- 是否可以在同一个配置文件上?
- 什么会导致我的服务器将所有请求重定向到 https
这是我的服务器nginx.conf
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/example.com.crt;
ssl_certificate_key /etc/ssl/example.com.key;
listen 80;
server_name example.com;
location /bla/bla {
proxy_pass http://example.com:3980/foo/bar1;
}
**location /bla2/**{
# proxy_pass http://example.com:3004;
return 301 http://example.com:3004$request_uri;
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-NginX-Proxy true;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
}
location /app3 {
rewrite ^/app3(.*) / break;
proxy_pass http://example.com:1236/app3;
}
}
我希望能够直接获取 http://example.com:3004 的内容,如果我将它放在浏览器中而无需任何重定向到 https。
仅当我尝试访问 example.com/bla2 时,我希望它需要 https 而不是 http,并被重定向到不同的路径。
您需要为此使用正则表达式和捕获组
location ~* /bla2/(.*) {
return 301 http://example.com:3004$is_args$args;
}
另一种方法是使用 rewrite
rewrite ^/bla2/(.*)$ http://example.com:3004/ redirect;
如果你想透明地代理它到 example.com:3004
删除 /bla2
那么你应该使用
location /bla2/ {
proxy_pass http://example.com:3004/;
}
/bla2/
和 http://example.com:3004/
中的尾部斜杠 /
在这里非常重要
我在我的服务器上设置了一个反向代理,里面有 nginx docker,从那时起,所有请求都重定向到 https,尽管我没有将所有位置都设置为 https 重定向。
基本上我想要的是能够使用反向代理服务 https 和 http。 此外,我希望能够动态地重定向到不同的 URI,例如,我想将 /bla2/foo/bar 的所有路由都设置为仅重定向到 /bla2 之后的内容 我试图到达这里的是,每当访问 example.com/bla2/foo/bar 时,它应该将其重定向到 example.com/foo/bar 没有 bla2 部分...
- 是否可以在同一个配置文件上?
- 什么会导致我的服务器将所有请求重定向到 https
这是我的服务器nginx.conf
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/example.com.crt;
ssl_certificate_key /etc/ssl/example.com.key;
listen 80;
server_name example.com;
location /bla/bla {
proxy_pass http://example.com:3980/foo/bar1;
}
**location /bla2/**{
# proxy_pass http://example.com:3004;
return 301 http://example.com:3004$request_uri;
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-NginX-Proxy true;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-Proto $scheme;
}
location /app3 {
rewrite ^/app3(.*) / break;
proxy_pass http://example.com:1236/app3;
}
}
我希望能够直接获取 http://example.com:3004 的内容,如果我将它放在浏览器中而无需任何重定向到 https。 仅当我尝试访问 example.com/bla2 时,我希望它需要 https 而不是 http,并被重定向到不同的路径。
您需要为此使用正则表达式和捕获组
location ~* /bla2/(.*) {
return 301 http://example.com:3004$is_args$args;
}
另一种方法是使用 rewrite
rewrite ^/bla2/(.*)$ http://example.com:3004/ redirect;
如果你想透明地代理它到 example.com:3004
删除 /bla2
那么你应该使用
location /bla2/ {
proxy_pass http://example.com:3004/;
}
/bla2/
和 http://example.com:3004/
中的尾部斜杠 /
在这里非常重要