【问题标题】:Nginx reverse proxy problem on an specified location指定位置的Nginx反向代理问题
【发布时间】:2021-02-09 18:08:46
【问题描述】:

我想按照以下说明访问其中一个 react 项目。

https://my.domain.com/app1  ==>  HTTP://localhost:7001

以下是 Nginx 配置:

server {
 listen 80;
server_name my.domain.com;
 return 301 https://$server_name$request_uri;
}
server {
 listen 443 ssl;

 ssl_certificate     /etc/letsencrypt/live/my.domain.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/my.domain.com/privkey.pem;
 ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
 ssl_ciphers         HIGH:!aNULL:!MD5;
 server_name my.domain.com;

 error_log /var/log/nginx/my.domain.com.err;
 access_log /var/log/nginx/my.domain.com.log;

location /app1 {
   proxy_set_header Host $http_host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Scheme $scheme;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
   add_header Pragma "no-cache";
   add_header Cache-Control "no-cache";

   proxy_pass http://localhost:7001/;
   proxy_redirect default;

   sub_filter_types *;
   sub_filter 'action="/'  'action="/app1/';
   sub_filter 'href="/'  'href="/app1/';
   sub_filter 'src="/'  'src="/app1/';
   sub_filter_once off;
 }
}

问题是某些源文件由https://my.domain.com/app1/... 响应,但有些由https://my.domain.com/... 响应,第二个源文件无法访问并给出HTTP 404 错误。

例如,以下来源已回复为HTTP 200

https://my.domain.com/app1/static/css/main.091g2s3f.chunk.css
https://my.domain.com/app1/static/js/2.29c551h6.chunk.js

以下回复为HTTP 404

https://my.domain.com/media/contact-title.png
https://my.domain.com/static/media/Yekan.05744gh2.woff
https://my.domain.com/logo.svg

有什么想法吗? 提前致谢。


我做了什么但没有解决:
我确实配置了Nginx rewrite,简单的proxy_passregex,但是问题没有解决,另外,我得到的所有回复都是HTTP 404

【问题讨论】:

    标签: reactjs nginx nginx-reverse-proxy


    【解决方案1】:

    您只是将/app1转发到上游,如果您想转发媒体和静态等,那么您需要更改以下内容

       location /app1 
    

       location /
    

    或者,如果您将它们作为静态文件提供,请添加以下内容

     location /static {
        root  /path/to/your/static/path;
      } 
    

    然后您可以从 example.com/static/something.css 调用所有静态文件

    【讨论】:

    • 我不想在这个项目中使用/。我需要在.../app1访问这个项目。
    • 然后将您的媒体等更改为 /app1/media
    • 实际上,我一直在寻找的是在不收取媒体或根 API 费用的情况下处理它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2016-02-12
    • 2020-12-18
    • 2019-05-17
    • 2019-03-14
    • 2014-10-27
    • 1970-01-01
    相关资源
    最近更新 更多