[英]Nginx url rewrite works only for /
Porting my angular app to nginx
.将我的 angular 应用程序移植到
nginx
。
My url rewrite config works only for the /
location.我的 url 重写配置仅适用于
/
位置。 If I go to localhost:5000/login
in the browser, I get a 404 error.如果我在浏览器中访问
localhost:5000/login
,则会收到 404 错误。
Here's my nginx.conf这是我的 nginx.conf
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 80;
root /usr/share/nginx/html;
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /usr/share/nginx/html/index.html;
}
}
}
The logs日志
2020/02/05 06:44:29 [error] 6#6: *1 open() "/usr/share/nginx/html/login" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /login HTTP/1.1", host: "localhost:5000"
172.17.0.1 - - [05/Feb/2020:06:44:29 +0000] "GET /login HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36" "-"
What am I missing?我错过了什么?
You need to set try_files
with the app root file.您需要使用应用程序根文件设置
try_files
。
try_files $uri $uri/ /usr/share/nginx/html/index.html;
Ref: https://angular.io/guide/deployment参考: https : //angular.io/guide/deployment
You have to use a regex on the location block, if not nginx only apply this location to the exact match.您必须在位置块上使用正则表达式,否则 nginx 仅将此位置应用于精确匹配。
location ~* /.* {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /usr/share/nginx/html;
}
You can see some examples and more info here您可以在此处查看一些示例和更多信息
The issue was including the default conf file with the following line,问题是包含以下行的默认 conf 文件,
include /etc/nginx/conf.d/*.conf
This was matching rules before it could hit my location
block.这是匹配规则,然后才能命中我的
location
块。 Removing this line got it working.删除这条线让它工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.