NGINX的虚拟子目录

时间:2018-04-17 19:27:50

标签: nginx url-rewriting location subdirectory

我已经测试了我的NGINX配置,可以在www.site.com上成功加载我的静态资产,重写等,路径为/ srv / public_html

现在,我要做的是传递可从www.site.com/us,www.site.com/ca,www.site.com/fr-ca等访问的虚拟子目录。它们继承了我已经拥有的规则,并且表现得好像文档根目录是/ src / public_html。

我尝试添加下面的代码,并且网站通过根index.php传递,但是,我的静态资产在重写中丢失。

location ~ ^\/((?<currentSite>us|us-es|ca|fr-ca|en-gb|fr|hz-hans|ja)\/) { 
    try_files $uri $uri/ @rewriteVirtualSubDirectories;
}

location @rewriteVirtualSubDirectories {
    rewrite ^\/((?<currentSite>us|us-es|ca|fr-ca|en-gb|fr|hz-hans|ja)\/)? /index.php?args;
}

location ~* \.php$ {
    try_files $uri =404; # if reference to php executable is invalid return 404
    expires off;
    fastcgi_pass php-fpm;
    fastcgi_keep_conn on;
    fastcgi_buffer_size 128k;
    fastcgi_buffers    4 256k;

    limit_req zone=php burst=20 nodelay;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param UNIQUE_ID $connection.$connection_requests;
    fastcgi_param HTTPS $http_x_ssl_offloaded if_not_empty;
}

1 个答案:

答案 0 :(得分:0)

看起来这解决了这个问题:

删除:

location ~ ^\/((?<currentSite>us|us-es|ca|fr-ca|en-gb|fr|hz-hans|ja)\/) { 
    try_files $uri $uri/ @rewriteVirtualSubDirectories;
}

location @rewriteVirtualSubDirectories {
    rewrite ^\/((?<currentSite>us|us-es|ca|fr-ca|en-gb|fr|hz-hans|ja)\/)? /index.php?args;
}

server范围添加

rewrite ^\/(?<currentSite>us|us-es|ca|fr-ca|en-gb|fr|hz-hans|ja)(?<realPath>.*)$  $realPath break;