【问题标题】:nginx - serving backend and frontend on different endpoints and same domainnginx - 在不同的端点和相同的域上提供后端和前端
【发布时间】:2017-01-21 13:55:56
【问题描述】:

我已经在 OS X 上安装了 nginxphp7.1mysql 等等......并且基本的测试示例正在运行.

当我尝试将 nginx 配置为在 user.domain.dev/api/... 上为 Laravel 后端user.domain.dev/... 上的 Angular 前端 提供服务时,我得到 404403。 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;
}

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

【问题讨论】:

  • staff 用户是否有权访问root 指令中指定的目录?如果不是,那将解释 403 错误。对于找不到的 index.php 文件,您尚未在 index 指令中指定它。你只有index.html

标签: angularjs laravel nginx frontend backend


【解决方案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 directiveif ($host ~* ^www\.(.*)) 可能应该替换为使用单独的 server 块。请参阅this document 了解更多信息。

location /api 块中的 try_files 语句包含不正确的默认 URI,它可能应该是 /api/index.php?$query_string

【讨论】:

    猜你喜欢
    • 2019-02-27
    • 1970-01-01
    • 2023-03-22
    • 2021-09-14
    • 2021-06-16
    • 2023-04-07
    • 2021-05-06
    • 2021-02-14
    • 1970-01-01
    相关资源
    最近更新 更多