在子文件夹中对/ dist /的简单Nginx重写

问题描述 投票:0回答:1

这是我当前用于特殊主机的nginx配置:

server {
    listen *:80;
    server_name example.test;
    root /var/www/example;
    index index.php index.htm index.html;
    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;

    if ($host ~* www\.(.*)) {
        set $host_without_www $1;
        rewrite ^(.*)$ $scheme://$host_without_www$1 permanent; #1
        #rewrite ^ $scheme://$host_without_www$1request_uri permanent; #2
    }

    # Rewrite for minify
    rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;

    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/start(/index)?|/index(.php)?)/?$)
    {
        rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # removes access to "system" folder, also allows a "System.php" controller
    if ($request_uri ~* ^/system)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # catch all
    error_page 404 /index.php;

    location / {
        root  /var/www/example;
        rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
    }

    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }
    location ~ /\.          { access_log off; log_not_found off; deny all; }
    location ~ ~$           { access_log off; log_not_found off; deny all; }

    location ~ \.php$ {
        root  /var/www/example;
        try_files  $uri  $uri/  /index.php?$args ;
        index  index.html index.htm index.php;
        fastcgi_index index.php;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param APP_ENV dev;

        fastcgi_pass docker-php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include fastcgi_params;

    }
}

我的分发文件位于/ var / www / example / templates / dist /中,但是我想将/ dist /的请求内部重写到/ templates / dist /文件夹中。

我尝试过这样的事情:

location /dist {
        root /var/www/example/templates/dist;
    }

没有成功。我总是通过index.php得到404]

我猜这不会是火箭科学。有人可以帮我吗? :)

nginx nginx-location nginx-config
1个回答
0
投票

找到解决方案:

location ~ ^/dist/(.*)$ {
        try_files $uri $uri/ /templates/dist/$1;
    }
© www.soinside.com 2019 - 2024. All rights reserved.