vam*_*man 3 nginx docker angular
我正在使用 Angular 2 应用程序、nginx 和 docker。每次我使用 /site 重新加载页面时,都会出现 404 错误。我的服务器块现在看起来像这样:
server {
listen 0.0.0.0:80;
listen [::]:80;
root /var/www/project/html;
index index.html;
server_name project.com;
location / {
try_files $uri $uri/ /index.html;
}}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多,并且看到了所有其他 stackoverflow 问题并尝试了所有可能性。但没有任何效果。有人可以帮忙吗?
更新:整个 nginx.conf:
user nginx;
worker_processes auto;
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;
include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)
启用/默认站点:
server {
listen 0.0.0.0:80;
listen [::]:80;
root /var/www/project/html;
index index.html;
server_name project.com;
location / {
try_files $uri $uri/ /index.html;
}}
Run Code Online (Sandbox Code Playgroud)
还有 Dockerfile:
FROM nginx
COPY ./docker/sites-enabled /etc/nginx/sites-enabled
COPY ./docker/nginx.conf /etc/nginx/nginx.conf
COPY ./dist /var/www/project/html
COPY ./dist /usr/share/nginx/html
EXPOSE 80
Run Code Online (Sandbox Code Playgroud)
在 nginx.conf 中,您将从两个位置加载附加配置:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
Run Code Online (Sandbox Code Playgroud)
第二个sites.enabled/default使用服务器名称加载您的配置project.com。
不过,第一个加载默认配置,default.conf该配置默认是 nginx docker 映像的一部分。该配置看起来类似于
server {
listen 80;
server_name localhost;
....
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
....
}
Run Code Online (Sandbox Code Playgroud)
因此,如果您尝试使用 访问您的站点localhost,则sites-enabled/default永远不会使用您的站点(因为您指定了 server_nameproject.com并且与 不匹配localhost)。相反,请求在 中运行,default.conf因为 server_name 是localhost。
在default.conf位置部分是:
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
Run Code Online (Sandbox Code Playgroud)
这意味着,如果您直接访问localhost,index.html就会得到服务并且一切都会按预期进行。但是一旦您尝试访问localhost/something,Nginx 就会尝试查找/usr/share/nginx/html/something不存在的文件/目录(-> 404)。
所以你必须选择:
从您的 nginx.conf 中删除include /etc/nginx/conf.d/*.conf;(或删除default.conf)并将您的 server_name 更改sites-enabled/default为localhost。然后你的请求将运行到你的配置中。
将其添加try_files $uri $uri/ /index.html;到您的位置,就像default.conf您在sites-enabled/default.
我会推荐第一个解决方案,不要default.conf在server_name您localhost的sites-enabled/config. 如果您稍后需要真实的域,您仍然可以使用正则表达式来匹配本地主机或您的域。
| 归档时间: |
|
| 查看次数: |
7332 次 |
| 最近记录: |