将Apache mod_proxy 转换为相当于Nginx的

我们在Apache中有这个工作。 你可以在这里看到http://screencast-o-matic.com/watch/cbQrFpI0S9 。

$ request_uri

/h_32/w_36/test.jpg

需要被路由到

/index.php/img/i/h_32/w_36/test.jpg

index.php会将请求路由到“img”控制器和“我”方法,然后处理图像并返回。 但是,我的MVC工作的REQUEST_URI。 所以简单地重写url将无法正常工作。 REQUEST_URI需要修改。

您可以在最后一个位置块中看到我正在传递修改后的REQUEST_URI,但是Nginx正在尝试打开/var/www/vhosts/ezrshop.com/htdocs/h_32/w_36/test.jpg(请参阅下面的错误日志 )扔404。

Nginx是不是应该把它发送给index.php? 为什么404?

root /var/www/vhosts/example.com/htdocs; index index.php index.html; set $request_url $request_uri; location ~ (h|w|fm|trim|fit|pad|border|or|bg)_.*\.(jpg|png)$ { if ($request_uri !~ "/img/i/") { set $request_url /index.php/img/i$1.$2; } try_files $uri $uri/ /index.php/img/i$1.$2; } location / { try_files $uri $uri/ /index.php$uri?$args; } location ~ ^(.+\.php)(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param CI_ENV production; #CI environment constant fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_param REQUEST_URI $request_url; } 

错误日志:

日志1: “/ img / i /”不匹配“/h_32/w_36/test.jpg”,请求:“GET /h_32/w_36/test.jpg HTTP / 1.1”

日志2:打开()“/var/www/vhosts/ezrshop.com/htdocs/h_32/w_36/test.jpg”失败(2:没有这样的文件或目录),请求:“GET / h_32 / w_36 / test。 jpg HTTP / 1.1“

这是解决scheme。

 root /var/www/vhosts/example.com/htdocs; index index.php index.html; location ~ ^/(h|w|fm|trim|fit|pad|border|or|bg)_.*\.(jpg|png)$ { include fastcgi_params; # route to /img/i/ fastcgi_param REQUEST_URI /img/i$uri; fastcgi_param SCRIPT_FILENAME $document_root/index.php; fastcgi_param CI_ENV production; #CI environment constant fastcgi_pass 127.0.0.1:9000; } location / { try_files $uri $uri/ /index.php$uri?$args; } location ~ ^(.+\.php)(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param CI_ENV production; #CI environment constant fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 

我会用下面的方法:

 location ~* ^/_img/(h|w|fm|trim|fit|pad|border|or|bg)(_.+\.)(jpg|png)$ { try_files $uri $uri/ /index.php/img/i/$1$2$3?$args; } 

我们使用location指令中的正则expression式来将我们想要的部分捕获到variables中,然后使用这些variables进行重写。

由于location正则expression式是以这种详细的方式定义的,所以它不会与重写的位置相匹配。 但是,在这里我们直接将更改的URL传递给try_files