ton*_*o94 16 linux nginx reverse-proxy
我nginx
用作反向代理,当我登录 Web 界面时,我被重定向到代理 URL。我想避免它并始终保留“server_name”作为 URL。是否可以?
这是我的/etc/nginx/conf.d/my_app.conf
:
server {
listen 443 ssl;
server_name my-app.net;
ssl_certificate /etc/pki/tls/certs/my-app.cer;
ssl_certificate_key /etc/pki/tls/private/my-app.key;
ssl_protocols TLSv1.1 TLSv1.2;
access_log /var/log/nginx/my-app.access.log main;
location / {
proxy_pass http://ip_of_the_app:7180/;
proxy_redirect off;
}
}
Run Code Online (Sandbox Code Playgroud)
我连接http://my-app.net
,输入登录信息,然后我被重定向到http://ip_of_the_app:7180
同一个登录页面,我必须再次登录。可以避免这种双重登录吗?
gro*_*mal 30
不要设置proxy_redirect
为off
,这不是在做您认为正在做的事情。 proxy_redirect
执行类似于 URL 重写的操作,例如:
location /sales/ {
proxy_pass http://ip_of_the_app:7180/;
proxy_redirect http://ip_of_the_app:7180/ http://$host/sales/;
}
Run Code Online (Sandbox Code Playgroud)
这允许您在/sales/
其他地方托管路径。但即便如此, 的默认参数proxy_redirect
也可以免费为您做到这一点。默认值是将位置重定向到任何存在的位置proxy_pass
(并且在您根本不设置proxy_redirect
或使用时使用默认参数proxy_redirect default;
)。
您不需要设置proxy_redirect
.
您缺少的是需要发送到应用程序的标头。其中最重要的是HOST
。这将根据需要执行代理并在浏览器中保留正确的 URL。
location / {
proxy_pass http://ip_of_the_app:7180/;
proxy_set_header HOST $host;
}
Run Code Online (Sandbox Code Playgroud)
请注意,应用程序http://ip_of_the_app:7180/
现在将接收带有Host: my-app.net
标头的请求。
您还应该考虑使用更多的标题:
proxy_set_header Referer $http_referer;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
Run Code Online (Sandbox Code Playgroud)
这将允许在http://ip_of_the_app:7180/
. X-Forwarded-For
给出实际客户端的 IP(而不是nginx
s IP)并X-Forwarded-Proto
检查客户端是否nginx
通过 HTTP 或 HTTPS连接到。