如何使用 nginx 从子目录为第二个应用程序提供服务?

如何使用 nginx 从子目录为第二个应用程序提供服务?

过去几天我一直在努力解决这个问题,希望有对 nginx 有更深入了解的人能为我指明正确的方向。

我需要使用 PHP-FPM 从现有 PHP 应用程序“根”内的子目录提供辅助 PHP 应用程序。

结构如下

- /var/www/www.example.com/
  - public/
    - index.php
  - subapplication/
    - public/
      - index.php

这两个应用程序均可从同一域www.example.com和获得www.example.com/subapplication/

我可以直接使用 nginx 为这些应用程序提供服务,但我正在努力使我的指令发挥作用,以便能够正确地为主要应用程序和子应用程序提供服务。

这是我目前感觉最接近的配置,但它仍然不能完全工作。

server {
    listen 80 default_server;
    server_name www.example.com;

    set $base /var/www/www.example.com;

    index index.php;

    location /subapplication/ {
        alias $base/subapplication/public;
        try_files $uri /subapplication/index.php?$query_string;
        location ~ \.php$ {
            # 404
            try_files $fastcgi_script_name =404;

            # default fastcgi_params
            include fastcgi_params;

            # fastcgi settings
            fastcgi_pass        unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index       index.php;
            fastcgi_buffers     8 16k;
            fastcgi_buffer_size 32k;

            # fastcgi params
            fastcgi_param DOCUMENT_ROOT     $realpath_root;
            fastcgi_param SCRIPT_FILENAME   $realpath_root$fastcgi_script_name;
        }
    }

    location / {
        root $base/public;
        try_files $uri $uri/ /index.php?$query_string;
        location ~ \.php$ {
            # 404
            try_files $fastcgi_script_name =404;

            # default fastcgi_params
            include fastcgi_params;

            # fastcgi settings
            fastcgi_pass        unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index       index.php;
            fastcgi_buffers     8 16k;
            fastcgi_buffer_size 32k;

            # fastcgi params
            fastcgi_param DOCUMENT_ROOT     $realpath_root;
            fastcgi_param SCRIPT_FILENAME   $realpath_root$fastcgi_script_name;
        }
    }
}

我是否遗漏了一些显而易见的东西?

404目前,对于所有发往该/subapplication/路由的请求,我只会得到一个。$uri/在下方try_files添加后location /subapplication/,系统会提示403我无法索引该subapplication/public文件夹。

我将非常感激任何人能够提供的见解。

答案1

作为对以后遇到此问题的任何人的标志,有一个长期存在的问题,aliastry_files令我烦恼。

解决方案可以在Richard Smith 的回答

我的工作配置:

# www.example.com configuration #

server {
    listen 80 default_server;
    server_name www.example.com;

    set $base /var/www/www.example.com;
    root $base/public;

    index index.php;

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

    # @SEE https://serverfault.com/a/828579/212904 #
    location ^~ /subapplication {
        alias $base/subapplication/public;

        if (!-e $request_filename) {
            rewrite ^ /subapplication/index.php last;
        }

        location ~ \.php$ {
            if (!-f $request_filename) {
                rewrite ^ /subapplication/index.php last;
            }

            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

答案2

尝试以下操作

server {
        listen 80;

        root /var/www/www.example.com;

        index index.php index.html index.htm index.php index.nginx-debian.html;

        server_name www.example.com;

        location ~* ^.+\.(js|css|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off;
                add_header Access-Control-Allow-Origin *;
                expires 30d;
        }

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

        location = /xmlrpc.php {
              return 404;
        }

        location /subapplication {
            try_files $uri $uri/subapplication /subapplication/index.php?q=$uri&$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location  /. {
                return 404;
        }
}

相关内容