2017-09-28 27 views
0

我正在使用角度爲2的應用程序,nginx和docker。每次我重裝與/網站它給了我一個頁面404.我的服務器塊貌似現在這種權利:nginx.conf中的try_files不起作用

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

我已經嘗試了很多,看到了一切其他計算器的問題,並嘗試各種可能性。但沒有任何工作。有人可以幫忙嗎?

UPDATE: 整個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 
+0

你有沒有錯誤日誌? – Moema

+0

@Moema只有錯誤日誌:'無法加載資源:服務器響應狀態爲404(未找到)'。沒有其他的。 – vaman

+0

我的意思是你可以在你的nginx.conf中配置的Nginx錯誤日誌(請參閱https://www.nginx.com/resources/admin-guide/logging-and-monitoring/)我認爲,默認情況下它們位於/ var/log/nginx/ – Moema

回答

0

在你nginx.conf ,您正在從兩個位置加載其他配置:

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

第二個加載您的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; 
    } 

    .... 
} 

所以,如果你嘗試用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/defaultlocalhost。然後你的請求會進入你的配置。

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

我建議第一個解決方案,不包括default.conf,改變你的server_namelocalhostsites-enabled/config。如果您以後需要您的真實域名,您仍然可以使用正則表達式來匹配localhost或您的域名。

+0

非常感謝。你是我的救命稻草 – vaman