2016-09-13 93 views
1

我已經安裝了nginx的在OS X與php7.1MySQL的等等...和基本測試例的工作相同的域名。nginx的 - 服務於不同的終端後端和前端和

當我嘗試配置nginx的對user.domain.dev/...服務Laravel後端user.domain.dev/api/...角前端我得到或。 Nginx的錯誤日誌主要是

/Users/name/Projects/domain.dev/api.domain.dev/" is forbidden 

/Users/name/Projects/domain.dev/frontend.domain.dev/build/index.php" failed (2: No such file or directory) 

我似乎無法理解的nginx的位置指令,所以他們會指向一個錯誤的目錄。感謝您的任何建議或指導。

我的配置是:

nginx.conf

user name staff; 
worker_processes auto; 

events { 
    worker_connections 1024; 
} 

http { 
    include mime.types; 
    default_type application/octet-stream; 

    ssl_certificate ssl/nginx.crt; 
    ssl_certificate_key ssl/nginx.key; 

    access_log /usr/local/var/log/nginx/access.log; 
    error_log /usr/local/var/log/nginx/error.log; 

    sendfile on; 
    keepalive_timeout 5; 

    gzip on; 

    include servers/*.conf; 
} 

服務器/ domain.dev.conf

server { 
    listen 80; 
    listen 443 ssl http2 default_server; 

    server_name domain.dev *.domain.dev www.domain.dev; 

    charset utf-8; 

    # removes trailing slashes (prevents SEO duplicate content issues) 

    if (!-d $request_filename) 
    { 
     rewrite ^/(.+)/$ /$1 permanent; 
    } 

    # enforce NO www 

    if ($host ~* ^www\.(.*)) 
    { 
     set $host_without_www $1; 
     rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent; 
    } 

    ## 
    ## Backend HTTP server 
    ## 

    location /api { 

     root /Users/name/Projects/domain.dev/api.domain.dev; 
     index index.php; 

     try_files $uri $uri/ /index.php?$query_string; 

     location ~ \.php$ { 
        fastcgi_pass 127.0.0.1:9000; 
        fastcgi_split_path_info ^(.+\.php)(/.+)$; 
        try_files $fastcgi_script_name =404; 
        fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
        include fastcgi.conf; 
      } 
    } 

    ## 
    ## Frontend HTTP server 
    ## 

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { 
     access_log off; 
     expires max; 
    } 

    location = /favicon.ico { 
     access_log off; 
     log_not_found off; 
    } 

    location = /robots.txt { 
     access_log off; 
     log_not_found off; 
    } 

    location/{ 
     root /Users/name/Projects/domain.dev/frontend.domain.dev/build; 
     index index.html; 
    } 

    location ~ /\. { 
     deny all; 
    } 
} 
+0

'staff'用戶是否可以訪問'root'指令中指定的目錄?如果不是,那將解釋403錯誤。對於無法找到的'index.php'文件,您沒有在'index'指令中指定它。你只有'index.html'。 –

回答

1

的主要問題是不一致的使用root的指示。您有兩個文檔根目錄,每個應用程序一個,但只在您的兩個位置中指定。沒有在server { ... }塊級別指定root,因此if (!-d $request_filename)是無意義的,location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$將總是導致一個404響應等

this document理解如何nginx處理的請求。

但是,您應該設置server塊中的前端根,並使用location /api塊中的後端根來覆蓋它。

server { 
    root /Users/name/Projects/domain.dev/frontend.domain.dev/build; 

    location/{ ... } 
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { ... } 

    location ^~ /api { 
     root /Users/name/Projects/domain.dev/api.domain.dev; 
     ... 
    } 
} 

這將使後端文檔根在:/Users/name/Projects/domain.dev/api.domain.dev/api - 注意,URI總是附加到root

另請注意,^~修飾符用於使前綴位置優先於同級別的正則表達式位置。詳情請見this document

我不喜歡你設計中的裸體if塊。 if (!-d $request_filename)可能應該替換爲try_files directive,並且if ($host ~* ^www\.(.*))應該可以替換爲使用單獨的server塊。有關更多信息,請參見this document

您的location /api塊中的try_files聲明包含不正確的默認URI,它應該可能是/api/index.php?$query_string