转换 Nginx Conf 以在每个位置使用不同的根目录

转换 Nginx Conf 以在每个位置使用不同的根目录

我正在尝试修改我的 Nginx 配置,以便 /tablet 和 /mobile 位置从它们自己的根目录提供服务。

我相信我需要向我的位置添加别名定义,但不太确定具体细节。

server {
    listen 80 default_server;
    # listen [::]:80 default_server ipv6only=on;

    root /var/www/htdocs;
    index index.php index.html index.htm;

    server_name localhost;

    location /mobile {
        if (!-e $request_filename){
            rewrite ^/mobile/(.*)$ /mobile/index.php?$args last;
        }
    }

    location /tablet {
        if (!-e $request_filename){
            rewrite ^/tablet/(.*)$ /tablet/index.php?$args last;
        }
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass phpfpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
server {
    listen 80;
    server_name www.domain.com domain.com docker.domain.com ;

    root /var/www/htdocs;
    index index.php index.html index.htm;

    if ($http_x_forwarded_proto != 'https') {
        return 301 https://$server_name$request_uri;
    }

    location /mobile {
        if (!-e $request_filename){
            rewrite ^/mobile/(.*)$ /mobile/index.php?$args last;
        }
    }

    location /tablet {
        if (!-e $request_filename){
            rewrite ^/tablet/(.*)$ /tablet/index.php?$args last;
        }
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass phpfpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        if ($http_x_forwarded_proto != 'https') {
            return 301 https://$server_name$request_uri;
        }
        try_files $uri $uri/ /index.php?$args;
    }
}

因此我尝试添加以下内容:

 location /test {
      alias /var/www/htdocs/test;
    }

这正确地提供了来自指定位置的文件,但如果我回显服务器根目录,它会显示 /var/www/htdocs/。

相关内容