【问题标题】:try_files in nginx.conf not workingnginx.conf 中的 try_files 不起作用
【发布时间】:2017-09-29 17:08:42
【问题描述】:

我正在使用 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;
}}

我已经尝试了很多,并且已经看到了所有其他 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/*;
}

网站启用/默认:

    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

【问题讨论】:

  • 你有任何错误日志吗?
  • @Moema 只有错误日志:“加载资源失败:服务器响应状态为 404(未找到)”。没有别的了。
  • 我的意思是您可以在 nginx.conf 中配置的 Nginx 错误日志(请参阅nginx.com/resources/admin-guide/logging-and-monitoring)我认为默认情况下它们位于 /var/log/nginx/
  • 我必须在我的 docker 中打开日志,但它没有打开。甚至 vim 都说,那个 error.log 不是一个文件
  • 因此您的服务器块看起来绝对正确(假设您的 index.html 位于 /var/www/project/html) - 也许这是您的 docker 设置的问题。你能发布整个 nginx.conf 和你的 dockerfile 吗?

标签: angular docker nginx


【解决方案1】:

在您的 nginx.conf 中,您正在从两个位置加载其他配置:

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

第二个使用服务器名称project.com 加载您的sites.enabled/default 配置。

不过,第一个加载默认配置default.conf,默认情况下它是 nginx docker 映像的一部分。该配置看起来类似于

server {
    listen       80;
    server_name  localhost;

    ....

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    ....
}

因此,如果您尝试使用localhost 访问您的站点,则永远不会使用您的sites-enabled/default(因为您指定了服务器名称project.com 并且与localhost 不匹配)。相反,请求在 default.conf 中运行,因为那里的 server_name 是 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)。

所以你必须选择:

  1. 从您的 nginx.conf 中删除 include /etc/nginx/conf.d/*.conf;(或删除 default.conf)并将您的 sites-enabled/default 中的 server_name 更改为 localhost。然后您的请求将运行到您的配置中。

  2. 将try_files $uri $uri/ /index.html; 添加到default.conf 中的位置,就像在sites-enabled/default 中一样。

我会推荐第一个解决方案,不要包含 default.conf 并将您的 server_name 更改为 localhost 在您的 sites-enabled/config 中。如果您以后需要您的真实域,您仍然可以使用正则表达式来匹配 localhost 或您的域。

【讨论】:

  • 非常感谢。你是我的救星
  • 这是一个完美的解释。
  • 完美,非常有趣的插图:),这对我有帮助!
  • 这不工作服务器 { 听 0.0.0.0:80;听 [::]:80; default_type 应用程序/八位字节流; gzip打开; gzip_comp_level 6; gzip_vary on; gzip_min_length 1000; gzip_proxied 任何; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k; client_max_body_size 256M;根 /usr/share/nginx/html;位置 / { try_files $uri $uri/ /index.html =404; } }
猜你喜欢
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 2019-03-15
  • 2012-04-24
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 2018-04-01
相关资源
最近更新 更多