mar*_*tin 76 nginx react-router
我正在将我的反应应用程序从webpack-dev-server转换为nginx.
当我转到根网址"localhost:8080/login"时,我只得到一个404,在我的nginx日志中,我看到它正试图获取:
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" "-"
Run Code Online (Sandbox Code Playgroud)
我应该在哪里寻找解决方案?
我的路由器位反应看起来像这样:
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);
Run Code Online (Sandbox Code Playgroud)
我的nginx文件是这样的:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我知道大多数设置都有效,因为当我在没有登录的情况下访问localhost:8080时,我也会获得登录页面.这不是通过重定向到localhost:8080/login - 它是一些反应代码.
Tob*_*oby 182
nginx配置中的位置块应为:
location / {
try_files $uri /index.html;
}
Run Code Online (Sandbox Code Playgroud)
问题是对index.html文件的请求有效,但您目前还没有告诉nginx将其他请求转发到index.html文件.
fuw*_*hin 13
这是我的解决方案
简单尝试:
location / {
root /var/www/mysite;
try_files $uri /index.html;
}
Run Code Online (Sandbox Code Playgroud)
不再尝试使用非html类型:
location / {
root /var/www/mysite;
set $fallback_file /index.html;
if ($http_accept !~ text/html) {
set $fallback_file /null;
}
try_files $uri $fallback_file;
}
Run Code Online (Sandbox Code Playgroud)
不再尝试使用非html类型和目录(使其try_files与index和/或兼容autoindex):
location / {
root /var/www/mysite;
index index.html;
autoindex on;
set $fallback_file /index.html;
if ($http_accept !~ text/html) {
set $fallback_file /null;
}
if ($uri ~ /$) {
set $fallback_file /null;
}
try_files $uri $fallback_file;
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*lig 12
也许这不是这里的确切情况,但对于使用anddocker为他们的react项目运行容器的任何人来说,在我的情况下,我在我的 中有我需要的一切:react-routerwebpack-dev-servernginx.conf
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 404 /index.html;
location = / {
root /usr/share/nginx/html;
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Run Code Online (Sandbox Code Playgroud)
但是 nginx 仍然不会向 root 发送 404 错误/。在查看容器后,我注意到有一些配置/etc/nginx/conf.d/default.conf会以某种方式覆盖我的配置,因此在我的配置中删除该文件dockerfile解决了问题:
...
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf # <= This line solved my issue
COPY nginx.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Run Code Online (Sandbox Code Playgroud)
使用您的 / 块如下:
server {
listen 8080;
server_name localhost;
absolute_redirect off;
root /usr/share/nginx/html;
index index.html;
location / {
expires 5m;
add_header Cache-Control "public";
try_files $uri $uri/ /jobs/index.html;
}
}
Run Code Online (Sandbox Code Playgroud)
这是在实时服务器中对我有用的解决方案:
我刚刚遵循了本教程,并且使用基本的 Nginx 配置只是简单地配置了位置块。
location / {
try_files $uri $uri/ /index.html;
}
Run Code Online (Sandbox Code Playgroud)
nginx 配置中的位置块应该是这样的
location / {
root /path/to/root/dir;
index index.html index.htm;
try_files $uri $uri/ /index.html;
Run Code Online (Sandbox Code Playgroud)
将所有请求转发到index.html(您的React SPA APP)。
和
在package.json您的 React 应用程序项目中,添加"homepage":"http://your.website.go"以使嵌套路径转发到正确的静态文件。
{
"name": "React App",
"version": "0.0.0",
"homepage": "http://your.website.go",
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.1.1",
"react-scripts": "5.0.0",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34537 次 |
| 最近记录: |