Nginnx配置Yii 2高级应用程序模板

ser*_*hei 7 nginx yii2 nginx-location

我无法配置Nginx服务器,以便:

  • /index.php文件将被发送以响应"index.php"请求.
  • public_html/frontend/web/index.php文件将被发送以响应"admin/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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Art*_*yan 15

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

答案的其余部分是建议清单.

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

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

I.共享主机部署

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

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

  1. 将内容/backend/web移至/frontend/web/admin.
  2. 纠正脚本的路径/frontend/web/admin/index.php(index-test.php如果使用的话)

就是这样,你的后端在/adminURL 的同一个域.另外,请阅读有关cookie的指南的最后一部分.高级模板旨在为每个环境使用不同的域,因此该指南描述了共享主机的后端配置,以保持前端和后端的cookie分离.

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

II.Nginx配置

误区

我不是一个profressional nginx管理员,但我可以根据我的个人经验和文档描述你的配置有什么问题.不幸的是,我无法提供文档的链接,因为我目前的评级不允许我发布超过2个链接.

服务器上下文 root

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

server {
    # Beginning of your configuration
    # ...

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

    # The rest of your configuration
    # ...
}
Run Code Online (Sandbox Code Playgroud)

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

root 代替 alias

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

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

    # The rest of location
    # ...
}
Run Code Online (Sandbox Code Playgroud)

我建议你阅读相关文档nginx的location,rootalias指令.

工作但配置不佳

我发布这个带有注释的示例配置仅供您理解,而不是用于生产用途,我建议您将其应用于您的生产(直到您肯定它是安全和合理的).

它有效,但它有一个恼人的缺点:如果你直接请求它(如/admin/index.php),后端找不到Yii2入口脚本,所以它必须与enablePrettyUrlset to trueshowScriptNameset一起使用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
    # ...
}
Run Code Online (Sandbox Code Playgroud)

此外,在Yii2后端配置中baseUrlrequest组件添加属性并将其设置为/admin.

我希望我的回答可以帮助您部署Yii2高级项目并更多地了解nginx,不过您的问题已经有6个月了.