Jim*_*ong 15 post nginx html5-history reactjs react-router
我目前正在使用Nginx作为反向代理并为我的静态资产提供服务.我使用的是React Router的HashLocation设置,因为它是默认设置,它允许我刷新路由而没有任何问题,也不需要任何其他配置,但使用该设置的问题是url有/#/ prepending的必要性我的路线(例如http://example-app.com/#/signup).
我现在正在尝试切换到React Router的HistoryLocation设置,但我无法弄清楚如何正确配置Nginx以便为所有路由提供index.html(例如http://example-app.com/signup).
这是我最初的nginx设置(不包括我的mime.types文件):
nginx.conf
# The maximum number of connections for Nginx is calculated by:
# max_clients = worker_processes * worker_connections
worker_processes auto;
# Process needs to run in foreground within container
daemon off;
events {
worker_connections 1024;
}
http {
# Hide nginx version information.
server_tokens off;
# Define the MIME types for files.
include /etc/nginx/mime.types;
# Update charset_types due to updated mime.types
charset_types
text/xml
text/plain
text/vnd.wap.wml
application/x-javascript
application/rss+xml
text/css
application/javascript
application/json;
# Speed up file transfers by using sendfile() to copy directly
# between descriptors rather than using read()/write().
sendfile on;
# Define upstream servers
upstream node-app {
ip_hash;
server 192.168.59.103:8000;
}
include sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)
默认
server {
listen 80;
root /var/www/dist;
index index.html index.htm;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1d;
}
location @proxy {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_redirect off;
proxy_pass http://node-app;
proxy_cache_bypass $http_upgrade;
}
location / {
try_files $uri $uri/ @proxy;
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用HashLocation时,此设置工作正常,但在更改为HistoryLocation(我做出的唯一更改)之后,当尝试刷新子路径的URL时,我得到了404 Can not GET.
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
Run Code Online (Sandbox Code Playgroud)
在location /街区.这允许我刷新并直接访问作为顶部位置的路由,但现在我无法提交PUT/POST请求,而是不允许返回405方法.我可以看到请求没有被正确处理,因为我添加的配置现在将我的所有请求重写为/index.html,这就是我的API接收所有请求的地方,但我不知道如何实现两者都能够将我的PUT/POST请求提交给正确的资源,以及能够刷新和访问我的路由.
AJP*_*AJP 19
location / {
try_files $uri /your/index.html;
}
Run Code Online (Sandbox Code Playgroud)
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
我知道你的例子比较复杂,@proxy但上面的工作对我的应用来说很好.
| 归档时间: |
|
| 查看次数: |
13539 次 |
| 最近记录: |