问题:如何配置 Nginx 进行页面刷新/重新加载

我在后端服务器上部署了两个耳朵文件。一个带有网络上下文根“/”,另一个带有“/bli”。我正在使用 Nginx 将请求传递给后端服务器。假设 xyz 是外部 url,它通过 nginx 将请求传递给后端服务器。

这就是正在发生的事情 - 1)https://xyz- 工作正常。页面刷新/重新加载也可以正常工作 2)https://xyz/bli- 重定向到 myserver.com/bli/ 并出现错误 3)https://xyz/bli/- 工作正常但页面刷新/重新加载重定向到 myserver.com/bli/

myserver.com 在不可解析的 URL 中,所以我收到错误

我尝试包括以下指令 -

location /bli/{
      proxy_pass https://backend;
      proxy_set_header Host myserver.com;
}

这解决了重定向到 myserver.com 的问题。但它改为重定向到https://xyz:8011/bli/,这会给出错误。它将与https://xyz:443/bli/一起使用。不知道 8011 端口从哪里来的 URL。

我也试过这个,但它重定向到 nginx 上的索引文件-

location /{
try_files $uri $uri/index.html
}

我试过这个,但没有奏效-

location /{
try_files $uri $uri/bli /bli
}

这是我的conf文件

server { 
listen *:8011;
root /apps/nginx/con/htdocs;
status_zone application_8011;

error_page 404 /error404.html;
error_page 500 502 503 504 /error500.html;

location /error404.html {
root /apps/nginx/con/htdocs/error_b;
}
location /error500.html {
root /apps/nginx/con/htdocs/error_b;
}
location / {

proxy_pass https://backend;
proxy_set_header Host myserver.com;

}
location /index.html {
root /apps/nginx/con/htdocs;
}

}

刷新https://xyz:/bli/在末尾删除 / 并重定向到 myserver.com/bli/ 。有没有办法可以避免在刷新时删除 / 以便刷新https://xyz:/bli/。或者我需要在我的配置文件中包含什么来解决页面刷新问题。

解答

以下解决方案对我有用 -

location /{
      proxy_pass https://backend;
      proxy_set_header Host myserver.com;
      port_in_redirect off
}

location /bli/{
          proxy_pass https://backend/bli/;
          proxy_set_header Host myserver.com;
          port_in_redirect off
}
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐