【问题标题】:Nginx config for Yii 2 Advanced App TemplateYii 2 高级应用模板的 Nginx 配置
【发布时间】:2014-08-26 10:57:42
【问题描述】:

我想以这样的方式配置 Nginx 网络服务器:

  • /index.php URI 的请求应由public_html/frontend/web/index.php 处理
  • /admin/index.php URI 的请求应由public_html/backend/web/index.php 处理

请在我错的地方提出建议。这是我的配置:

server {
    listen        80;
    server_name   yii2.lo;
    server_tokens off;

    client_max_body_size 128M;
    charset       utf-8;

    access_log    /var/log/nginx/yii2-access.log main buffer=50k;
    error_log     /var/log/nginx/yii2-error.log notice;

    set           $host_path      "/srv/http/yii2/public";
    set           $yii_bootstrap  "index.php";

    index         $yii_bootstrap;

    location / {
        root          $host_path/frontend/web;
        try_files $uri $uri/ /$yii_bootstrap?$args;

    }

    location /admin {
        root          $host_path/backend/web;
        try_files $uri $uri/ /$yii_bootstrap?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index           $yii_bootstrap;

        # Connect to php-fpm via socket
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;

        fastcgi_connect_timeout     30s;
        fastcgi_read_timeout        30s;
        fastcgi_send_timeout        60s;
        fastcgi_ignore_client_abort on;
        fastcgi_pass_header         "X-Accel-Expires";

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  HTTP_REFERER     $http_referer;
        include fastcgi_params;
    }

    location ~* \.(js|css|less|png|jpg|jpeg|gif|ico|woff|ttf|svg|tpl)$ {
        expires 24h;
        access_log off;
    }

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

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

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

【问题讨论】:

    标签: nginx yii2 nginx-location


    【解决方案1】:

    长话短说:使用下面提供的第一种方法。

    答案的其余部分是建议列表。

    我将把我的答案分成两部分。 在第一部分中,我将告诉您根据您所需的 URL 请求实现目标的最简单和最快的方法,但它会部分破坏应用程序结构,但并不严重。

    在第二部分中,我将向您描述您在配置文件中出错的地方,并向您展示一个编写不佳的配置,以满足您的需求。

    我。共享主机部署

    我强烈建议您使用它。这是来自 Yii 2 文档的 official way 以使后端在同一域中工作,尽管它有助于将项目部署到共享主机。而且它不需要任何额外的 nginx 配置,只需要前端 root 的基本配置。

    让我根据这个指南写一个简单的列表:

    1. 将内容从/backend/web 移动到/frontend/web/admin
    2. 更正 /frontend/web/admin/index.php(和 index-test.php,如果您使用它)中的脚本路径

    就是这样,您的后端位于/admin URL 的同一域中。此外,请阅读指南中有关 cookie 的最后一节。高级模板旨在为每个环境使用不同的域,因此该指南描述了共享主机的后端配置,以将 cookie 与前端和后端分开。

    当然,不要忘记修改您的 /environments 文件,以便使用 /init 脚本正确初始化您的项目。

    二。 Nginx 配置

    错误

    我不是专业的 nginx 管理员,但我可以根据我的个人经验和文档描述您的配置中的问题。不幸的是,我无法提供文档链接,因为我目前的评级不允许我发布超过 2 个链接。

    服务器上下文root

    您的服务器上下文中没有root 指令。因此,当~ \.php$ 位置匹配时,它根本没有root 并使用默认的nginx 根。尝试在server 上下文中设置常见的root 指令,然后默认情况下所有位置都会有它。例如:

    server {
        # Beginning of your configuration
        # ...
    
        root /srv/http/yii2/public/frontend/web;
    
        # The rest of your configuration
        # ...
    }
    

    没有更高的上下文根是常见的pitfall

    root 而不是 alias

    其次,当一个位置匹配时,uri 被附加到该位置的根目录,这就是服务器试图寻找的路径。因此,您的/admin 位置建议服务器搜索$host_path/backend/web/admin。在您的情况下,您应该使用 alias 指令告诉服务器匹配的位置 uri 指的是别名路径,而不是附加到根目录:

    location /admin {
        alias          $host_path/backend/web;
    
        # The rest of location
        # ...
    }
    

    我建议您阅读有关 locationrootalias 指令的相关 nginx 文档。

    工作但编写不佳的配置

    我发布这个带有 cmets 的示例配置仅供您理解,而不是用于生产用途,我不鼓励您将其应用到您的生产中(直到您确定它是安全且可靠的)。

    它可以工作,但它有一个烦人的缺陷:如果你直接请求它,后端找不到Yii2入口脚本(如/admin/index.php),所以它必须与enablePrettyUrl设置为trueshowScriptName一起使用到false,但是它会在后端 Web 根目录中找到任何其他 PHP 脚本。

    server {
        # The beginning of your configuration
        # ...
    
        # By default we will provide frontend
        root /srv/http/yii2/public/frontend/web;
        index index.php;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location /admin {
            # We use /web/index here to make backend call to php scripts
            # distinct from frontend call
            index /web/index.php;
            alias $root_base/backend/web;
            try_files $uri $uri/ /web/index.php?$args;
    
            # Rewrite PHP requests from /admin to /web
            # However, Yii2 entry script returns 404
            location ~ ^/admin/.*\.php$ {
                rewrite ^/admin/(.*)$ /web/$1;
            }
    
        }
    
        location ~ ^/web/.*\.php$ {
            # Make sure this location cannot be called externally
            internal;
    
            # Remember, that the uri of this location
            # will be appended to this root!
            root $root_base/backend;
    
            # PHP settings for backend
        }
    
        location ~ \.php$ {
            # PHP settings for frontend
        }
    
        # The rest of your configuration
        # ...
    }
    

    另外,将baseUrl 属性添加到Yii2 后端配置中的request 组件并将其设置为/admin

    我希望我的回答能帮助你部署你的 Yii2 高级项目和更多地了解 nginx,不过你的问题已经 6 个月了。

    【讨论】:

    • 除了推荐的方法(#1)之外,如果想要启用漂亮的url,你可能需要这样做:location /admin { try_files $uri $uri/ /admin/index.php?$args; }
    【解决方案2】:

    这是我的工作配置,基于接受的答案。我的项目backend目录改名为admin

    # Example config for nginx
    # frontend is available on yii-application.local/
    # backend (admin) is available on yii-application.local/admin
    # make sure that @app/frontend/config/main.php and @app/admin/config/main.php components sections are configured properly
    # e.g. @app/frontend/config/main.php
    #   'homeUrl' => '',
    #   ...
    #   'components' => [
    #         'request' => [
    #              'baseUrl' => '',
    #          ],
    #          'urlManager' => [
    #              'enablePrettyUrl' => true,
    #              'showScriptName' => false,
    #          ],
    #   ]
    #
    # e.g. @app/admin/config/main.php
    #   'homeUrl' => '/admin',
    #   ...
    #   'components => [
    #        'request' => [
    #            'baseUrl' => '/admin',
    #        ],
    #        'urlManager' => [
    #            'enablePrettyUrl' => true,
    #            'showScriptName' => false,
    #        ],
    #   ]
    server {
        set $project_root /home/yii/apps/yii-advanced;
        set $fcgi_server unix:/opt/php/var/run/php5-fpm.sock;
    
        charset utf-8;
        client_max_body_size 128M;
    
        listen 80;
        server_name yii-application.local;
    
        root $project_root/frontend/web;    
        index index.php;
    
        access_log  /home/yii/apps/yii-advanced/logs/access-backend.log;
        error_log   /home/yii/apps/yii-advanced/logs/error-backend.log;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location /admin {
            index /web/index.php;
            alias $project_root/admin/web;
            try_files $uri $uri/ /web/index.php?$args;
    
            location ~ ^/admin/.*\.php$ {
                rewrite ^/admin/(.*)$ /web/$1;
                fastcgi_pass $fcgi_server;
                include fastcgi.conf;
            }
        }
    
        location ~ ^/web/.*\.php$ {
            internal;
            root $project_root/admin;
            fastcgi_pass $fcgi_server;
            include fastcgi.conf;
        }
    
        location ~* \.php$ {
            try_files $uri =404;
            fastcgi_pass $fcgi_server;
            include fastcgi.conf;
        }
    
        location ~* \.(htaccess|htpasswd|svn|git) {
            deny all;
        }
    }
    

    【讨论】:

    • 我正在使用这个设置,除了资产目录(或除 .php 文件之外的任何文件)。我遇到了 404 所有此类文件。有没有 ida?
    【解决方案3】:

    尝试指定配置 Nginx : 我使用“高级”模板的配置 在域配置文件中,为so指定前端:

    收听frontend.site.loc:80; # 前端

    指定后端域: 收听 backend.site.loc:80; # 用于后端

    【讨论】:

      【解决方案4】:

      这家伙在创建高级应用 nginx 配置方面做得非常好(带有子域,恕我直言是最好的设置):https://gist.github.com/Kison/45ec9ce3c1ebf422cbd42bd5ce04d8e4

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-06
        • 2021-10-28
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多