简体   繁体   English

Nginx:子文件夹的位置

[英]Nginx: location for subfolder

I want to move my root project to subfolder.我想将我的根项目移动到子文件夹。

My current config is:我目前的配置是:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

And current project structure as expected:和预期的当前项目结构:

/var/www/public
 ﹂index.php

The problem is to rewrite locations ~ \.php$ and / with new variable of subfolder (project name).问题是用子文件夹的新变量(项目名称)重写位置~ \.php$/

/var/www/new-project/public
 ﹂index.php

I've tried to rewrite locations but it didn't work:我试图重写位置,但没有奏效:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    # root /var/www/public;
    root /var/www;

    location ~ ([a-zA-Z0-9_\/\.]+)\.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^$1(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~/([a-zA-Z0-9_\/\.]+)$ {
        alias /var/www/$1/public;
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

I think second location is pretty valid but I can't get how to rewrite it for \.php$ ending.我认为第二个位置非常有效,但我不知道如何为\.php$结尾重写它。

Found some overcome for this problem:找到了一些克服这个问题的方法:

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/;

    location / {
        deny all;
        return 404;
    }

    location ~ /([a-zA-Z0-9-_]+)/(.+\.php$) {
        alias /var/www/$1/public/$2;
        # try_files $uri =404;
        fastcgi_split_path_info ^/$1/(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/$1/public/$2/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /([a-zA-Z0-9-_]+)/$ {
        alias /var/www/$1/public/;
        # try_files $uri $uri/ /index.php?$query_string;s
        gzip_static on;
    }
}

But the only one disadvantage of this approach that you can not use try_files with the alias .但是这种方法的唯一一个缺点是您不能将try_filesalias一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM