在 Nginx 上模拟 Apache 目录别名

Emulating Apache Directory Alias on Nginx

使用 Apache 我们可以做这样的事情:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/app1/public
        <Directory />
                Options +Indexes +FollowSymLinks +MultiViews
                AllowOverride FileInfo
                Require all granted
        </Directory>

        Alias /app2 "/var/www/app2/public"
        <Directory "/var/www/app2/public">
                DirectoryIndex index.php
                Options +Indexes +FollowSymLinks +MultiViews
                AllowOverride All
                Require all granted
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

因此,在这种方法中,我有两个不同的应用程序:

两者都是使用 Laravel(PHP 框架)开发的,需要接收 QUERY STRING 才能工作。

我想移动到 Nginx,目前我的配置是这样的:

server {

    listen 80;
    server_name host.com;
    root /var/www/app1/current/public;

    index index.php;
    charset utf-8;

    # App 1
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ ^/index\.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

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

    access_log off;
    error_log  /var/log/nginx/app.log error;

    sendfile off;
    client_max_body_size 100m;

    location ~ /\.ht {
        deny all;
    }
}

app1(根应用程序)工作正常。问题是:如何在该配置文件中设置 app2

我试过:

# App 2
location ^~ /app2 {
    alias /var/www/app2/current/public;
    try_files $uri $uri/ /index.php?$query_string;
}

但是,没有成功。

我尝试的很少。

# App 2
# Possibly no need for regex, this will capture urls 
# /app2 and /app2/anything/else
location /app2 {
    alias /var/www/app2/current/public;
    try_files $uri $uri/ /index.php?$query_string;
}

这可能是您唯一的问题并且可能会解决它。

但是!如果仍然存在问题(没有输入文件错误,或者如果它仍然转到 app1),那么我们有使用 /index.phptry_files 的复杂因素。这使得它继续到你的 location ~ ^/index\.php$ { 块,它可能抓住了错误的 $document_root.

在这种情况下,我不确定最好的办法是不使用别名,而是使用两个 PHP 块。希望 alias 能够自动更改 $documentroot

对于那些不明白我想要什么的人:我需要在同一个域名(没有子域)上托管多个 PHP 应用程序。

终于解决了这个问题。这是我的最终配置:

server {

    listen 80 deferred;

    server_name server.com;
    index index.php;
    charset utf-8;

    # App 1 (main app)
    location / {
        root /var/www/app1/current/public;
        try_files $uri $uri/ /index.php?$query_string;

        error_log /var/log/nginx/app1.notice.log notice;
        error_log /var/log/nginx/app1.error.log error;

        location ~* ^/index\.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/app1/current/public/index.php;
            fastcgi_intercept_errors off;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
        }
    }

    # App 2
    location ~* /app2 {
        alias /var/www/app2/current/public;
        try_files $uri $uri/ /app2/index.php?$query_string;

        error_log /var/log/nginx/app2.notice.log notice;
        error_log /var/log/nginx/app2.error.log error;

        location ~* ^/app2/index\.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/app2/current/public/index.php;
            fastcgi_intercept_errors off;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
        }
    }

    # Files
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # Error
    access_log off;
    rewrite_log on;

    # Disable .htaccess access
    location ~ /\.ht {
        deny all;
    }
}