nginx-在不同的端点和相同的域上服务后端和前端 [英] nginx - serving backend and frontend on different endpoints and same domain

查看:52
本文介绍了nginx-在不同的端点和相同的域上服务后端和前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have installed nginx on OS X with php7.1, mysql and so on... and the basic testing example is working.

当我尝试将nginx配置为在 user.domain.dev/api/... Angular前端上提供 Laravel后端 user.domain.dev/...我收到 404 403 .Nginx错误日志主要是

When I try to configure nginx to serve Laravel backend on user.domain.dev/api/... and Angular frontend on user.domain.dev/... I am getting 404 or 403. Nginx error log is mostly

/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的location指令,因此它们指向错误的目录.感谢您的任何建议或指导.

I can't seem to understand nginx's location directive and so they're pointing to a wrong directories. Thanks for any advices or guidance.

我的配置是:

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

推荐答案

主要问题是 root 指令的用法不一致.您有两个文档根,每个应用程序一个,但只能在您的两个位置中指定.在 server {...} 块级别没有指定 root ,因此 if(!-d $ request_filename)是没有意义的,位置〜* \.(jpg | jpeg | gif | css | png | js | ico | html)$ 始终会导致 404 响应,依此类推.

The main problem is an inconsistent use of the root directive. You have two document roots, one for each application, but only specified in two of your locations. There is no root specified at the server { ... } block level, therefore if (!-d $request_filename) is meaningless, location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ will always result in a 404 response, etc.

阅读本文档,以了解 nginx 处理请求.

Read this document to understand how nginx processes a request.

但是您可能应该在 server 块中设置前端根,并在 location/api 块中用后端根覆盖它.

But you should probably set the frontend root in the server block and override it with the backend root in the location /api block.

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 would place the backend document root at: /Users/name/Projects/domain.dev/api.domain.dev/api -- note that the URI is always appended to the root.

还请注意, ^〜修饰符用于使前缀位置优先于同一级别的正则表达式位置.有关详细信息,请参见本文档.

Note also, the ^~ modifier is used to make the prefix location take precedence over regular expression locations at the same level. See this document for details.

我不喜欢设计中裸露的 if 块. if(!-d $ request_filename)应该替换为本文档.

I do not like the naked if blocks in your design. The if (!-d $request_filename) should probably be replaced with a try_files directive, and if ($host ~* ^www\.(.*)) should probably be replaced by using a separate server block. See this document for more.

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

The try_files statement in your location /api block contains an incorrect default URI, it should probably be /api/index.php?$query_string.

这篇关于nginx-在不同的端点和相同的域上服务后端和前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆