英文:
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 & 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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论