我正在将一个带有 React Router 的 ReactJs 项目部署到 Debian Jessie 系统,同时在端口 3001 上运行后端 API 节点服务。
基本上我需要这些规则:
所有 /api/* 路由必须重定向到
- 所有 /api/* 路由必须重定向到 localhost:3001
- /graphql 路由也必须重定向到 localhost:3001
- 除静态文件(图像等)外,所有其他路由都必须重定向到 /var/www/html/index.html。
这是我的 nginx 配置:
server {
listen 80 default_server;
root /var/www/html;
index index.html index.htm;
location /api {
proxy_redirect http://localhost:3001/ /api;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_intercept_errors on;
proxy_pass http://127.0.0.1:3001;
}
location /graphql {
proxy_redirect http://localhost:3001/ /graphql;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_intercept_errors on;
proxy_pass http://127.0.0.1:3001;
}
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
# access_log logs/static.log; # I don't usually include a static log
}
location ~* \.(?:css|js)$ {
try_files $uri =404;
expires 1y;
access_log off;
add_header Cache-Control "public";
}
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
# Any route that doesn't have a file extension (e.g. /devices)
location / {
try_files $uri $uri/ /index.html;
}
}
访问/api/*
路由确实被重定向到代理,但不是/graphql
所有 /graphql 访问都返回404 not found
。
我更像是一名程序员,所以我坚持尝试部署该项目。非常感谢您的帮助...