简体   繁体   English

nginx.conf 中的 try_files 不起作用

[英]try_files in nginx.conf not working

I'm working with an angular 2 app, nginx and docker.我正在使用 angular 2 应用程序、nginx 和 docker。 Everytime I'm reloading a page with /site it gives me a 404. My server block looks like this right now:每次我用 /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;
}}

I have tried a lot, and have seen ALL other stackoverflow questions and have tried every possibility.我已经尝试了很多,并且已经看到了所有其他 stackoverflow 问题并尝试了所有可能性。 But nothing works.但没有任何作用。 Can someone please help?有人可以帮忙吗?

UPDATE: The whole nginx.conf:更新:整个 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/*;
}

the sites-enabled/default:站点启用/默认:

    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;
}}

And the Dockerfile:和 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

In your nginx.conf, you are loading additional configs from two locations:在您的 nginx.conf 中,您正在从两个位置加载其他配置:

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

The second one loads your sites.enabled/default config with the server name project.com .第二个使用服务器名称project.com加载您的sites.enabled/default配置。

The first one, though, loads the default config default.conf which is part of the nginx docker image per default.但是,第一个加载默认配置default.conf ,默认情况下它是 nginx docker 映像的一部分。 That config looks similar to该配置看起来类似于

server {
    listen       80;
    server_name  localhost;

    ....

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

    ....
}

So if you try to access your site with localhost , your sites-enabled/default is never used (since you specified the server_name project.com and that doesn't match with localhost ).因此,如果您尝试使用localhost访问您的站点,则永远不会使用您的sites-enabled/default (因为您指定了 server_name project.com并且与localhost不匹配)。 Instead the request runs in the default.conf since there the server_name is localhost .相反,请求在default.conf运行,因为 server_name 是localhost

And in the default.conf the location part is:default.conf ,位置部分是:

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

That means, if you just go to localhost , the index.html is served and everything works as expected.这意味着,如果您只是转到localhost ,则会提供index.html并且一切都按预期工作。 But as soon as you try accessing localhost/something , Nginx is trying to find the file/directory /usr/share/nginx/html/something which doesn't exist (-> 404).但是,一旦您尝试访问localhost/something ,Nginx 就会尝试查找不存在的文件/目录/usr/share/nginx/html/something (-> 404)。

So you have to options:所以你必须选择:

  1. Remove include /etc/nginx/conf.d/*.conf;删除include /etc/nginx/conf.d/*.conf; from your nginx.conf (or delete the default.conf ) and change the server_name in your sites-enabled/default to localhost .从您的 nginx.conf (或删除default.conf )并将您的sites-enabled/default的 server_name 更改为localhost Then your request will run into your config.然后您的请求将运行到您的配置中。

  2. Add the try_files $uri $uri/ /index.html;添加try_files $uri $uri/ /index.html; to the location in the default.conf like you have it in your sites-enabled/default .default.conf的位置,就像在您的sites-enabled/default

I would recommend the first solution, do not include the default.conf and change your server_name to localhost in your sites-enabled/config .我会推荐第一个解决方案,不包括default.conf并将您的server_name更改为localhost在您的sites-enabled/config If you later need your real domain, you can still use regex to match for either localhost or your domain.如果您以后需要真正的域,您仍然可以使用正则表达式来匹配 localhost 或您的域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM