簡體   English   中英

在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>

因此,在這種方法中,我有兩個不同的應用程序:

  • 應用1(主應用)-> www.mydomain.com/something
  • 應用2(另一個子應用)-> 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;
}

但是,沒有成功。

我想嘗試的幾件事。

# 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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM