【发布时间】:2019-11-11 09:32:17
【问题描述】:
我正在使用 nginx(反应前端)提供我的静态文件。 我为每个页面使用了不同的网址,例如 /login /user /home。
我的 nginx 不会将这些重定向到我的 index.html 文件。
我做了一些更改,现在我也无法访问我的主页。它返回无法获取 /。
这是我的 nginx.conf 文件。我在 Windows 上运行它。我的 index.html 在 build 文件夹中。如何让我的nginx在reactjs中使用Router和Link,这样我可以通过引用链接或者导航栏来获取页面?
worker_processes 5;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name ip;
location / {
root /Users/username/projectfolder/build;
index index.html index.htm;
try_files $uri $uri/ index.html;
proxy_pass http://ipaddress:portnumber;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}
更新:
我试过下面的配置
server {
listen some_port;
server_name some_ip;
root C:\\nginx-1.17.1\\build\\;
index index.html index.htm;
location /test/ {
alias C:\\nginx-1.17.1\\build\\;
index index.html index.htm;
try_files $uri \\index.html;
#internal;
#return index;
}
location = /index.html {
# no try_files here
return 502;
}
location / {
#root C:\\Users\\DEV-a0a02yc\\insurance_automation\\build;
try_files $uri $uri\ \index.html?$args;
#try_files $uri/ index.html;
proxy_pass http://some_ip:some_port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
在 /test/ url 中,我得到一个空白页和一个重写/内部重定向错误说:
2019/07/01 09:53:22 [error] 17820#18008: *1 rewrite or internal redirection cycle while internally redirecting to "/favicon.ico\ \index.html\ \index.html\ \index.html\ \index.html\ \index.html\ \index.html\ \index.html\ \index.html\ \index.html\ \index.html\ \index.html"
如何在此处提供我的 index.html 文件以在输入此 url 时处理重定向?
【问题讨论】:
标签: reactjs nginx nginx-location nginx-config