使用NGinx将图像文件重定向到Uri

时间:2018-11-24 13:41:55

标签: macos nginx

我要将Laravel网站从Apache(htaccess)切换到NGinx。我已经建立了一个图像服务,该图像服务会生成一个带有适当参数的uri来调整ex:pics/images/max24h/music_video_red_icon.png的大小。 我Apache找不到文件,它将重定向到路由image/images/max24h/music_video_red_icon.png,在laravel中,Action在其中创建并返回图像。在.htaccess中,它可以使用以下代码:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pics(.*)\.(jpe?g|png|gif|ico|bmp)$ /image$1.$2 [R=307,L]

现在在NGinx conf中,如何正确重定向?我尝试了很多建议,例如:

# Redirect pics file not found to php laravel route to create the image
location @img_proxy {
    rewrite ^/pics(.*)\.(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
}

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

它根本不起作用,如果我删除了这些行,服务器就可以工作。我正在使用Mac High Sierra 10.13.6。可能是一些配置冲突?这是完整的nginx.conf

user _www _www;

worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen 80 default_server;
        server_name localhost;
        root /var/www/megalobiz/public;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";

        index index.html index.htm index.php;

        charset utf-8;

        gzip on;
            gzip_vary on;
            gzip_disable "msie6";
            gzip_comp_level 6;
            gzip_min_length 1100;
            gzip_buffers 16 8k;
            gzip_proxied any;
            gzip_types
                text/plain
                text/css
                text/js
                text/xml
                text/javascript
                application/javascript
                application/x-javascript
                application/json
                application/xml
                application/xml+rss;

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        error_page 404 /index.php;

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

        # Redirect pics file not found to php laravel route to create the image
        location ~ ^/pics(.*)\.(jpe?g|png|gif|ico|bmp)$ {
            try_files $uri @img_proxy;
        }

        location @img_proxy {
            rewrite ^/pics(.*)\.(jpe?g|png|gif|ico|bmp)$ /image$1.$2;
        }

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

        location ~ \.php$ {
            try_files      $uri =404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)\$ {
            expires 1M;
            access_log off;
            add_header Cache-Control "public";
        }
        location ~* \.(?:css|js)\$ {
            expires 7d;
            access_log off;
            add_header Cache-Control "public";
        }

        location ~ /\.(?!well-known).* {
            deny all;
        }

        location ~ /\.ht {
            deny  all;
        }
    }

    include servers/*;
}

1 个答案:

答案 0 :(得分:1)

.htaccess文件的功能与您的rewrite语句之间的主要区别在于,Apache将使用307响应执行外部重定向,而Nginx则执行内部重定向。有关详细信息,请参见this document

要强制Nginx执行外部重定向,请将permanentredirect标志附加到rewrite语句,该语句分别生成301或302响应。

例如:

location @img_proxy {
    rewrite ^/pics(.*)\.(jpe?g|png|gif|ico|bmp)$ /image$1.$2 redirect;
}

302和307响应之间的区别在于,后者将重定向POST请求方法,而无需将其更改为GET。

如果您需要重定向POST请求,则需要使用正则表达式 locationif块捕获URI的相关部分,并使用{{ 1}}。

例如:

return 307

有关location @img_proxy { if ($uri ~ ^/pics(.*)\.(jpe?g|png|gif|ico|bmp)$) { return 307 /image$1.$2$is_args$args; } } 的使用,请参见this caution