指定位置的Nginx反向代理问题



我想按照下面的指令访问一个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

任何想法?提前谢谢。


我做了什么,没有解决:
我确实配置了Nginxrewrite,简单的proxy_passregex,但问题没有解决,此外,我得到了所有的响应作为HTTP 404

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

location /app1 

location /

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

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

那么你可以从example.com/static/something.css

调用所有的静态文件

最新更新