Angular 尝试nginx.conf中的\u文件不工作
我正在使用angular 2应用程序,nginx和docker。每次我用/site重新加载页面时,它都会给我一个404。我的服务器块现在看起来像这样:Angular 尝试nginx.conf中的\u文件不工作,angular,docker,nginx,Angular,Docker,Nginx,我正在使用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; }}
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;
}}
我已经尝试了很多,看到了所有其他的问题,尝试了所有的可能性。但什么都不管用。有人能帮忙吗
更新:
整个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/*;
}
已启用/默认的站点:
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;
}}
以及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
在nginx.conf中,您正在从两个位置加载其他配置:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
第二个加载服务器名为project.com
的sites.enabled/default
config
但是,第一个加载默认配置default.conf
,这是nginx docker映像的一部分。该配置类似于
server {
listen 80;
server_name localhost;
....
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
....
}
因此,如果您尝试使用localhost
访问您的站点,您的站点启用/默认值
将永远不会被使用(因为您指定的服务器名称project.com
与localhost
不匹配)。相反,请求在default.conf
中运行,因为那里的服务器名称是localhost
在default.conf
中,位置部分是:
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
这意味着,如果您只需转到localhost
,就会提供index.html
,并且一切正常。但只要您尝试访问localhost/something
,Nginx就会试图找到不存在的文件/目录/usr/share/Nginx/html/something
(->404)
因此,您必须选择:
include/etc/nginx/conf.d/*.conf从nginx.conf中选择code>(或删除default.conf
),并将站点中启用的/default
中的服务器名称更改为localhost
。然后,您的请求将运行到您的配置中
try_文件$uri$uri//index.html
到default.conf
中的位置,就像您在站点中启用了/default
一样我建议第一种解决方案,不要包括
default.conf
,并在站点启用/config
中将服务器名称更改为localhost
。如果您以后需要您的真实域,您仍然可以使用regex来匹配localhost或您的域。您有任何错误日志吗?@Moema只有错误日志:“加载资源失败:服务器响应状态为404(未找到)”。没有别的。我指的是可以在Nginx.conf中配置的Nginx错误日志(请参阅),我认为默认情况下,它们位于/var/log/Nginx/I,我必须在docker中打开日志,但它没有打开。即使vim说,error.log也不是一个文件,因为您的服务器块看起来绝对正确(假定index.html位于/var/www/project/html),这可能是docker设置的问题。你能发布整个nginx.conf和你的dockerfile吗?非常感谢。你是我的救命恩人这是一个完美的解释。完美,非常有趣的例子:),这对我很有帮助!此服务器不工作{listen 0.0.0.0:80;listen[::]:80;默认类型应用程序/八位字节流;gzip打开;gzip组件级别6;gzip更改打开;gzip最小长度1000;gzip代理任何;gzip类型文本/纯文本/css应用程序/json应用程序/x-javascript文本/xml应用程序/xml应用程序/xml+rss文本/javascript;gzip_缓冲区16 8k;客户端最大大小256M;root/usr/share/nginx/html;位置/{try_files$uri$uri//index.html=404;}}