我在为托管 2 个独立应用程序的 nginx 定义正确的 vhost 规则时遇到问题。不幸的是,我无法在主机上使用子域,因此我只能使用子文件夹方法。主要应用程序是 TYPO3,具有以下 vhost 配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/typo3dev/web;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_read_timeout 1200;
}
location ~ /\.(js|css)$ {
expires 604800s;
}
if (!-e $request_filename){
rewrite ^/(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ /$1.$3 last;
}
location ~* ^/fileadmin/(.*/)?_recycler_/ {
deny all;
}
location ~* ^/fileadmin/templates/.*(\.txt|\.ts)$ {
deny all;
}
location ~* ^/typo3conf/ext/[^/]+/Resources/Private/ {
deny all;
}
location ~* ^/(typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) {
}
location ~* ^/qa/.* {
allow all;
}
location / {
if ($query_string ~ ".+") {
return 405;
}
if ($http_cookie ~ 'nc_staticfilecache|be_typo_user|fe_typo_user' ) {
return 405;
} # pass POST requests to PHP
if ($request_method !~ ^(GET|HEAD)$ ) {
return 405;
}
if ($http_pragma = 'no-cache') {
return 405;
}
if ($http_cache_control = 'no-cache') {
return 405;
}
error_page 405 = @nocache;
try_files /typo3temp/tx_ncstaticfilecache/$host${request_uri}index.html @nocache;
}
location @nocache {
try_files $uri $uri/ /index.php$is_args$args;
}
}
现在,我想在 TYPO3 根目录的子文件夹中托管第二个应用程序packagist/packagist/web
。此 Packagist 应用程序需要重写 URL 规则才能正常工作。其正常 vhost 配置如下:
server {
listen 80;
server_name yourserver.loc;
root /var/www/packagist/web;
index index.php index.html;
location / {
# try to serve file directly, fallback to rewrite
try_files $uri @rewriteapp;
satisfy any;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/app\.php(/|$) {
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/packagist_error.log;
access_log /var/log/nginx/packagist_access.log;
}
我如何合并这 2 个配置?我试图将以下内容添加到我的 TYPO3 vhost,但 URL 重写不起作用。
location ^~ /packagist/packagist/web/.* {
#root /var/www/typo3dev/web/packagist/packagist/web;
alias /var/www/typo3dev/web/packagist/packagist/web;
index index.php index.html;
#try_files $uri $uri/ /packagist/packagist/web/app.php;
try_files $uri @rewrite;
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
}
location @rewrite {
rewrite ^(.*)$ /app.php/$1 last;
}
目前,如果我想使用 URL 例如http://example.com/packagist/packagist/web/app.php/register我收到 404 页面。
请帮忙 :)
答案1
这可能无法解决您的问题,但显然是配置错误:
location ^~ /packagist/packagist/web/.*
在您的解决方案中不起作用。^~
是前缀匹配,导致 nginx 在前缀匹配后不检查正则表达式。因此,在这种情况下,它会匹配所有以 literal 开头的 URI 请求/packagist/packagist/web/.*
,我怀疑您的应用程序中是否有任何类似的请求。
您需要使用它location ^~ /packagist/packagist/web
来匹配以该前缀开头的所有请求。