Yii 2 高级应用模板的 Nginx 配置 [英] Nginx config for Yii 2 Advanced App Template

查看:28
本文介绍了Yii 2 高级应用模板的 Nginx 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to configure the Nginx web-server in such a way that:

  • /index.php URI 的请求应由 public_html/frontend/web/index.php
  • 处理
  • /admin/index.php URI 的请求应由 public_html/backend/web/index.php
  • 处理
  • Requests to the /index.php URI should be handled by public_html/frontend/web/index.php
  • Requests to the /admin/index.php URI should be handled by public_html/backend/web/index.php

请指教我错的地方.这是我的配置:

Advice please where I'm wrong. Here is my config:

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

推荐答案

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

Long story short: use the first method provided below.

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

The rest of the answer is a list of recommendations.

我将把我的答案分成两部分.在第一部分中,我将根据您想要的 URL 请求告诉您实现目标的最简单和最快的方法,但它部分破坏了应用程序结构,不过没什么大不了的.

I'm going to separate my answer in two sections. In the first part, I will tell you the easiest and the fastest way to achieve your goal according to your desired URL requests, but it partly breaks the app structure, nothing serious, though.

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

In the second part, I will describe you where you made mistakes in your configuration file and I will show you a poorly written configuration for your needs which works.

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

I highly encourage you to use this. This is an official way from Yii 2 documentation to make backend work at the same domain, although it helps to deploy a project to a shared hosting. And it doesn't require any additional nginx configuration, just a basic one for frontend root.

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

Let me write a simple list according to this guide:

  1. 将内容从 /backend/web 移动到 /frontend/web/admin.
  2. 更正/frontend/web/admin/index.php(和index-test.php,如果你使用它)中的脚本路径
  1. Move contents from /backend/web to /frontend/web/admin.
  2. Correct scripts' paths in /frontend/web/admin/index.php (and index-test.php, if you use it)

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

That's all, you have your backend at the same domain at /admin URL. Additionally, read the last section of the guide regarding cookies. The advanced template was designed to use different domains for each environment, therefore the guide describes backend config for shared hosting to keep cookies from frontend and backend separate.

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

Of course, don't forget to modify your /environments files for proper initialization of your project with /init script.

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

I'm not a profressional nginx administrator, but I can describe what's wrong in your configuration based on my personal experience and the documentation. Unfortunately, I won't be able to provide links to the documentation, because my current rating won't allow me to post more than 2 links.

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

You do not have root directive in your server context. Thus, when ~ \.php$ location is matched, it doesn't have root at all and uses default nginx root. Try setting common root directive in the server context, then all locations will have it by default. For example:

server {
    # Beginning of your configuration
    # ...

    root /srv/http/yii2/public/frontend/web;

    # The rest of your configuration
    # ...
}

没有更高的上下文根是一个常见的陷阱.

Not having a higher context root is a common pitfall.

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

Secondly, when a location is matched, the uri is appended to the location's root and that's the path the server attempts to look for. Thus, your /admin location suggests that the server search for $host_path/backend/web/admin. In your situation, you should use alias directive which tells the server that the matched location uri refers to alias path, not appended to root:

location /admin {
    alias          $host_path/backend/web;

    # The rest of location
    # ...
}

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

I recommend that you read related nginx documentation about location, root and alias directives.

我发布带有评论的示例配置仅供您理解,不用于生产用途,我不鼓励您将其应用于您的生产(直到您确定它是安全无害的).

I post this sample configuration with comments for your understanding only, not for production use, I dicourage you to apply it for your production (until you're positive it's safe and sound).

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

It works, but it has an annoying defect: backend cannot find Yii2 entry script if you request it directly (like /admin/index.php), so it must be used with enablePrettyUrl set to true and showScriptName set to false, however it finds any other PHP script in the backend web root.

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.

Additionally, add baseUrl property to the request component in your Yii2 backend config and set it to /admin.

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

I hope my answer will help you deploying your Yii2 advanced project and understanding nginx more, nevertheless your question is 6 months old.

这篇关于Yii 2 高级应用模板的 Nginx 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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