我使用nginx将目录代理到远程服务器,并使用以下代码:
location /directory/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host sub.domain.io;
proxy_pass http://sub.domain.io:80/;
}然而,像CSS这样的各种静态文件由于后面的其他规则而中断。
有什么方法可以结束与此目录匹配的处理吗?同样,您也可以使用last进行重写。
下面是完整的配置:
root /var/www/site;
autoindex off;
index index.php;
charset utf-8;
log_not_found off;
access_log /var/www/site/data/logs/access.log;
error_log /var/www/site/data/logs/error.log;
add_header X-debug-message "$geoip_country_code" always;
location ~ \.php$
{
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~ "^/(de|es|fr)*$" {
set $lang "$1/";
set $page 'homepage';
if ($http_accept_encoding !~ gzip) {
rewrite ^/(.*)$ /index.php?request=/ last;
}
try_files /data/cache/html/$lang$page.html.gz @php;
add_header Content-Encoding gzip;
gzip off;
default_type text/html;
}
location /directory/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host sub.domain.io;
proxy_pass http://sub.domain.io:80/;
}
if ($allowed_country = no) {
return 444;
}
location /
{
try_files $uri @main_cache;
}
location @main_cache {
if ($http_accept_encoding !~ gzip) {
rewrite ^/(.*)$ /index.php?request=$1 last;
}
if ( $query_string ) {
rewrite ^/(.*)$ /index.php?request=$1 last;
}
try_files /data/cache/html$uri.html.gz @php;
add_header Content-Encoding gzip;
gzip off;
default_type text/html;
}
location @php {
rewrite ^/(.+)$ /index.php?request=$1 last;
}
location /data/cache/pred
{
try_files $uri $uri/ =404;
if ( !-e $request_filename )
{
rewrite /cache/ps/(.*)$ /index.php?request=get&pin=$1 last;
break;
}
}
location ~* \.(eot|ico|jpe?g|png|svg|ttf|woff|woff2)$
{
gzip_static on;
gzip_vary on;
expires 30d;
add_header Cache-Control "public";
}
location ~* \.(css|js)$
{
gzip_static on;
gzip_vary on;
expires 7d;
add_header Cache-Control "public";
}
location ~ ^/(src|cron|tpl)
{
return 301 $scheme://$server_name;
}请求最终成为源服务器上的404。对于倒数第二块中列出的文件格式,它只会这样结束。
发布于 2020-08-10 05:48:10
答案在nginx位置指令文档中。
一个简短的版本适用于您的情况:
在处理URL时,nginx首先将/directory视为候选。它会记住这个匹配,然后尝试查找正则表达式匹配。
如果找到合适的正则表达式location,nginx将应用该位置规则。如果找不到匹配,nginx将使用记住的/directory位置。
在您的示例中,正则表达式块与/directory中的一些URI相匹配。当您想要防止它时,可以使用以下块:
location ^~ /directory/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host sub.example.com;
proxy_pass http://sub.example.com:80/;
}https://serverfault.com/questions/1029454
复制相似问题