0

I am using nginx to proxy a directory to a remote server, with the code:

location /directory/ {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   Host      sub.domain.io;
    proxy_pass         http://sub.domain.io:80/;
}

However, various static files like CSS break due to other rules I have after this.

Is there a way I can end the processing once matched to this directory? In the same way you can use last for rewrites.

Here is the full config:

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;
}

The request ends up as a 404 on the source server. It only ends up like that for the file formats listed in the penultimate two blocks.

Kohjah Breese
  • 171
  • 2
  • 13

1 Answers1

1

The answer is in nginx location directive documentation.

A short version applied to your situation:

nginx first sees /directory as a candidate when processing the URL. It remembers this match, and then tries to look for regular expression matches.

If a suitable regular expression location is found, nginx will apply that location rules. If a match is not found, nginx will use the remembered /directory location.

In your case, regular expression blocks match some of the URIs inside /directory. When you want to prevent it, you can use the following block:

location ^~ /directory/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host sub.example.com;
    proxy_pass http://sub.example.com:80/;
}
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63