Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Nginx上模拟Apache目录别名_Php_Laravel_Nginx_Virtualhost - Fatal编程技术网

Php 在Nginx上模拟Apache目录别名

Php 在Nginx上模拟Apache目录别名,php,laravel,nginx,virtualhost,Php,Laravel,Nginx,Virtualhost,使用Apache我们可以执行以下操作: <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/app1/public <Directory /> Options +Indexes +FollowSymLinks +MultiViews AllowOverride File

使用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>
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.php
try_文件一起使用的复杂因素。这会使它转到
位置~^/index\.php${
块,它可能抓取了错误的
$document\u 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;
    }
}

你打开了我的心扉。我对我找到的最终解决方案感到高兴。谢谢!
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;
    }
}