React-Router和Nginx [英] React-router and nginx
问题描述
我正在将我的react应用从webpack-dev-server过渡到nginx.
I am transitioning my react app from webpack-dev-server to nginx.
当我进入根URL"localhost:8080/login"时,我只是得到一个404,在我的nginx日志中,我看到它正在尝试获取:
When I go to the root url "localhost:8080/login" I simply get a 404 and in my nginx log I see that it is trying to get:
my-nginx-container | 2017/05/12 21:07:01 [error] 6#6: *11 open() "/wwwroot/login" failed (2: No such file or directory), client: 172.20.0.1, server: , request: "GET /login HTTP/1.1", host: "localhost:8080"
my-nginx-container | 172.20.0.1 - - [12/May/2017:21:07:01 +0000] "GET /login HTTP/1.1" 404 169 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0" "-"
我应该在哪里寻找修复程序?
Where should I look for a fix ?
我的路由器中的bit看起来像这样:
My router bit in react looks like this:
render(
<Provider store={store}>
<MuiThemeProvider>
<BrowserRouter history={history}>
<div>
Hello there p
<Route path="/login" component={Login} />
<App>
<Route path="/albums" component={Albums}/>
<Photos>
<Route path="/photos" component={SearchPhotos}/>
</Photos>
<div></div>
<Catalogs>
<Route path="/catalogs/list" component={CatalogList}/>
<Route path="/catalogs/new" component={NewCatalog}/>
<Route path="/catalogs/:id/photos/" component={CatalogPhotos}/>
<Route path="/catalogs/:id/photos/:photoId/card" component={PhotoCard}/>
</Catalogs>
</App>
</div>
</BrowserRouter>
</MuiThemeProvider>
</Provider>, app);
我的Nginx文件是这样的:
And my nginx file like this:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server {
listen 8080;
root /wwwroot;
location / {
root /wwwroot;
index index.html;
try_files $uri $uri/ /wwwroot/index.html;
}
}
}
我知道大多数设置都可以使用,因为当我不登录而进入localhost:8080时,我也会获得登录页面.这不是通过重定向到localhost:8080/login-它是一些响应代码.
I know that most of the setup works because when I go to localhost:8080 without being logged in I get the login page as well. this is not through a redirect to localhost:8080/login - it is some react code.
推荐答案
nginx配置中的位置块应为:
The location block in your nginx config should be:
location / {
try_files $uri /index.html;
}
问题在于,对index.html文件的请求可以正常工作,但是您目前还没有告诉nginx也将其他请求转发给index.html文件.
The problem is that requests to the index.html file work, but you're not currently telling nginx to forward other requests to the index.html file too.
这篇关于React-Router和Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!