我想这样配置Nginx web服务器:
- 对
- 对
/admin/index.php
URI的请求应该由public_html/backend/web/index.php
处理
/index.php
URI的请求应该由public_html/frontend/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;
}
}
长话短说:使用下面提供的第一个方法
答案的其余部分是一个推荐列表。
我将把我的答案分成两部分。在第一部分中,我将告诉你根据你想要的URL请求实现目标的最简单和最快的方法,但它在一定程度上破坏了应用程序的结构,尽管没有什么严重的问题。
在第二部分,我将描述你在配置文件中犯错误的地方,我将向你展示一个写得很差的配置,以满足你的需要。
<标题>。共享主机部署我强烈建议你使用这个。这是Yii 2文档中让后端在同一域中工作的官方方式,尽管它有助于将项目部署到共享主机上。它不需要任何额外的nginx配置,只需要一个基本的前端根配置。
让我按照这个指南写一个简单的列表:
- 将
/backend/web
的内容移动到/frontend/web/admin
。 - 正确的脚本路径在
/frontend/web/admin/index.php
(和index-test.php
,如果你使用它)
就是这样,你有你的后端在同一个域名在/admin
URL。此外,请阅读关于cookie的指南的最后一部分。高级模板被设计为为每个环境使用不同的域,因此该指南描述了共享主机的后端配置,以保持前端和后端cookie分开。
当然,不要忘记修改您的/environments
文件,以便使用/init
脚本正确初始化您的项目。
我不是专业的nginx管理员,但我可以根据我的个人经验和文档描述您的配置中的错误。不幸的是,我将无法提供链接到文档,因为我目前的评级不允许我发布超过2个链接。
服务器环境root
在你的服务器上下文中没有root
指令。因此,当~ .php$
位置匹配时,它根本没有根,而是使用默认的nginx根。尝试在server
上下文中设置通用root
指令,然后所有位置默认都有它。例如:
server {
# Beginning of your configuration
# ...
root /srv/http/yii2/public/frontend/web;
# The rest of your configuration
# ...
}
没有更高的上下文根是一个常见的陷阱。
root
代替alias
其次,当匹配位置时,uri 被附加到位置的根目录,这是服务器试图查找的路径。因此,您的/admin
位置建议服务器搜索$host_path/backend/web/admin
。在您的情况下,您应该使用alias
指令,它告诉服务器匹配的位置uri引用别名路径,而不是附加到根:
location /admin {
alias $host_path/backend/web;
# The rest of location
# ...
}
我建议你阅读有关location
, root
和alias
指令的相关nginx文档。
工作但写得不好的配置
我发布了这个示例配置的注释,仅供您理解,而不是用于生产使用,我鼓励您将其应用于您的生产(直到您确定它是安全可靠的)。
它工作,但它有一个恼人的缺陷:后端不能找到Yii2入口脚本,如果你直接请求它(像/admin/index.php
),所以它必须与enablePrettyUrl
设置为true
和showScriptName
设置为false
一起使用,但是它发现任何其他PHP脚本在后端web根。
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个月前的
标题>标题>这是我的工作配置,基于接受的答案。我的项目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;
}
}
尝试指定配置Nginx:我使用"高级"模板的配置在域配置文件中,为so指定前端:
听frontend.site.loc: 80;# for frontend
指定后端域:听backend.site.loc: 80;# for backend
这家伙在创建高级应用nginx配置方面做得很好(我认为子域是最好的设置):https://gist.github.com/Kison/45ec9ce3c1ebf422cbd42bd5ce04d8e4