【发布时间】:2019-01-29 21:44:31
【问题描述】:
我有一个使用路由的angular 2 应用程序。当我点击我的基本 URL 时,页面会正确加载并路由到下一页。但是,如果我刷新同一页面,则会引发404 error。例如,如果我输入我的基本网址 example.com 然后它会路由到 example.com/dashboard 但如果我刷新此页面则会导致 404 错误。
我的Nginx 配置设置文件中有以下内容:
server {
ssl on;
ssl_certificate Certificate.cer;
ssl_certificate_key privateKey.key;
listen 443 ssl;
server_name example.com;
location / {
include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://localhost/temp/;
}
location /app {
include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://12.34.56.78:5001;
}
}
刷新页面时出现404错误。基于this link 我修改了我的Nginx 配置以添加try_files 部分,如下所示:
更新:
server {
ssl on;
ssl_certificate Certificate.cer;
ssl_certificate_key privateKey.key;
listen 443 ssl;
server_name example.com;
location / {
include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://localhost/temp/;
alias /usr/share/nginx/html/proj1;
try_files $uri $uri/ /index.html$is_args$args;
}
location /app {
include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://12.34.56.78:5001;
}
}
现在它会抛出无法找到JS 文件的错误。我在index.html(例如<script src="app.js"></script>)文件中引用的所有JS 文件都存在于与index.html 相同的目录中。
编辑:这是我在 Chrome 控制台的网络选项卡中看到的 JS 错误之一:
**General:**
Request URL: https://example.com/script.js
Request Method: GET
Status Code: 404 Not Found
Referrer Policy: no-referrer-when-downgrade
**Response Header:**
Connection: keep-alive
Content-Length: 571
Content-Type: text/html
Date:
Server: nginx/1.12.2
【问题讨论】: