首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用nginx从子目录服务第二个应用程序?

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

Server Fault用户
提问于 2020-03-31 21:12:07
回答 2查看 2.4K关注 0票数 1

在过去的几天里,我一直在与此斗争,我希望有更深层次的理解的人能为我指明正确的方向。

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

结构是

代码语言:javascript
复制
- /var/www/www.example.com/
  - public/
    - index.php
  - subapplication/
    - public/
      - index.php

这两个应用程序都可以从www.example.comwww.example.com/subapplication/的相同域中获得。

我可以直接使用nginx服务这些应用程序中的任何一个,但是我很难让我的指令正常工作,这样我就可以正确地为主应用程序和子应用程序服务。

这是我在这一点上最接近的配置,但仍然不太好用。

代码语言:javascript
复制
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;
        }
    }
}

我漏掉了什么明显的东西吗?

目前,我只需为所有对/subapplication/路由的请求获得一个D5。将$uri/添加到location /subapplication/下的try_files会给出一个403,说明它不能索引subapplication/public文件夹。

我会非常感激任何人所能提供的任何见解。

EN

回答 2

Server Fault用户

回答已采纳

发布于 2020-04-01 19:23:55

作为以后遇到这个问题的人的一个标杆,aliastry_files有一个长期存在的问题,一直困扰着我。

解决方案可以在理查德·史密斯的回答中找到

我的工作配置:

代码语言:javascript
复制
# 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
EN

Server Fault用户

发布于 2020-04-01 11:25:08

尝试以下几点

代码语言:javascript
复制
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;
        }
}
票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1010321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档