静态网页内容在NGINX重写后无法访问。

huangapple go评论57阅读模式
英文:

Static web content not accessible after NGINX rewrite

问题

I have a CentOS 7 VM,其中我部署了两个Spring服务:tasklist(8082)和operate(8081),这两个服务可以通过浏览器访问(我可以在浏览器中访问http://[主机IP]:8081(主机IP:VM IP地址)并访问应用程序):

但是,当我尝试反向代理这些端口(8081和8082)时,我无法访问静态内容,当我检查时,我得到了以下错误:

以下是我的Nginx配置:

server {
    listen 445;
    listen [::]:445;

    location /operate/ {
        rewrite ^/operate/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:8081;
    }

    location /tasklist/ {
        rewrite ^/tasklist/(.*)$ /$1 break;
        proxy_pass http://127.0.0.1:8082;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

<details>
<summary>英文:</summary>

I have a centos7 vm in which I deployed two spring services : tasklist (8082) and operate (8081), those two services are available via browser (I can go to http://[host-ip]:8081 (host-ip : vm ip address) via my browser and get access to the application) :

[![enter image description here][1]][1]

but when I tried to reverse proxy those ports (8081 &amp; 8082) I can not get access to the static content when I inspect I got those errors : 
[![enter image description here][2]][2]


here is my nginx configuration : 

server{
listen 445;
listen [::]:445;

   location /operate/ {
       rewrite ^/operate/(.*)$ /$1 break;
       proxy_pass http://127.0.0.1:8081;
   }

   location /tasklist/ {
       rewrite ^/tasklist/(.*)$ /$1 break;
       proxy_pass http://127.0.0.1:8082;
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }

}



  [1]: https://i.stack.imgur.com/5CRDQ.png
  [2]: https://i.stack.imgur.com/goSBR.png

</details>


# 答案1
**得分**: 1

将其转换为以下形式:

```nginx
location ~ ^/operate/(.*)$ {
    proxy_pass http://127.0.0.1:8081/$1;
}

location ~ ^/tasklist/(.*)$ {
    proxy_pass http://127.0.0.1:8082/$1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /static {
    root /var/www/html/static; # 根据您的实际文件夹路径进行调整
}
英文:

Convert it like this:

location ~ ^/operate/(.*)$ {
           proxy_pass http://127.0.0.1:8081/$1;
       }

location ~ ^/tasklist/(.*)$ {
           proxy_pass http://127.0.0.1:8082/$1;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       }

location /static {
  root /var/www/html/static; # Adjust to your actual folder
}

huangapple
  • 本文由 发表于 2023年5月30日 07:38:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定