在Nginx上模拟Apache Directory Alias

时间:2015-07-14 21:26:47

标签: php laravel nginx virtualhost

使用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>

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

  • App 1(主应用程序) - &gt; www.mydomain.com/something
  • App 2(另一个子应用程序) - &gt; www.mydomain.com/app2/login

两者都是使用Laravel(PHP Framework)开发的,需要接收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;
}

但是,没有成功。

2 个答案:

答案 0 :(得分:1)

我尝试过几件事。

# 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

答案 1 :(得分:1)

对于那些不完全理解我想要的人:我需要在同一个域名上托管多个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;
    }
}