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

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

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配置:

  1. server {
  2. listen 445;
  3. listen [::]:445;
  4. location /operate/ {
  5. rewrite ^/operate/(.*)$ /$1 break;
  6. proxy_pass http://127.0.0.1:8081;
  7. }
  8. location /tasklist/ {
  9. rewrite ^/tasklist/(.*)$ /$1 break;
  10. proxy_pass http://127.0.0.1:8082;
  11. proxy_set_header Host $host;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. }
  15. }
  1. <details>
  2. <summary>英文:</summary>
  3. 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) :
  4. [![enter image description here][1]][1]
  5. 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 :
  6. [![enter image description here][2]][2]
  7. here is my nginx configuration :

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

  1. location /operate/ {
  2. rewrite ^/operate/(.*)$ /$1 break;
  3. proxy_pass http://127.0.0.1:8081;
  4. }
  5. location /tasklist/ {
  6. rewrite ^/tasklist/(.*)$ /$1 break;
  7. proxy_pass http://127.0.0.1:8082;
  8. proxy_set_header Host $host;
  9. proxy_set_header X-Real-IP $remote_addr;
  10. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  11. }

}

  1. [1]: https://i.stack.imgur.com/5CRDQ.png
  2. [2]: https://i.stack.imgur.com/goSBR.png
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 将其转换为以下形式:
  7. ```nginx
  8. location ~ ^/operate/(.*)$ {
  9. proxy_pass http://127.0.0.1:8081/$1;
  10. }
  11. location ~ ^/tasklist/(.*)$ {
  12. proxy_pass http://127.0.0.1:8082/$1;
  13. proxy_set_header Host $host;
  14. proxy_set_header X-Real-IP $remote_addr;
  15. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  16. }
  17. location /static {
  18. root /var/www/html/static; # 根据您的实际文件夹路径进行调整
  19. }
英文:

Convert it like this:

  1. location ~ ^/operate/(.*)$ {
  2. proxy_pass http://127.0.0.1:8081/$1;
  3. }
  4. location ~ ^/tasklist/(.*)$ {
  5. proxy_pass http://127.0.0.1:8082/$1;
  6. proxy_set_header Host $host;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. }
  10. location /static {
  11. root /var/www/html/static; # Adjust to your actual folder
  12. }

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:

确定