2016-09-13 84 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