0

I'm serving wordpress behind a reverse proxy in nginx. I want to be able to serve also static pages that I have in a different root directory.

I tried this:

server {
    listen            80;
    listen       [::]:80;
    server_name ${NGINX_HOST};
    root /static-pages;
    sendfile          on;
    tcp_nodelay       on;

    location / {
        try_files $uri $uri/ /static-pages/ =404;
        #try_files $uri $uri/ =404;
        #try_files /static-pages $uri/ =404;
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_pass http://wordpress;

        proxy_set_header      X-Real-IP $remote_addr;
        proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header      Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;

    }
}

The wordpress works good but when I try to reach one of the static pages, I keep getting 404. Of course I can add a location directive to each static page I have but this seems like an overhead. Can I make it work somehow?

Here's a part of the debug log which I think is relevant:

[debug] 7#7: *2 generic phase: 12
[debug] 7#7: *2 try files handler
[debug] 7#7: *2 http script var: "/marketing-analyst"
[debug] 7#7: *2 trying to use file: "/marketing-analyst" "/static-pages/marketing-analyst"
[debug] 7#7: *2 http script var: "/marketing-analyst"
[debug] 7#7: *2 trying to use dir: "/marketing-analyst" "/static-pages/marketing-analyst"
[debug] 7#7: *2 try file uri: "/marketing-analyst"

Of course the directory /static-pages/marketing-analyst exists.

0

Almost there. The solution is first to try the static file, and go to the reverse proxy (wordpress) as a fallback:

server {
    error_log  /var/log/nginx/error.log debug;
    listen            80;
    listen       [::]:80;
    server_name ${NGINX_HOST};
    sendfile          on;
    tcp_nodelay       on;
    root /static-pages;

    location / {
        try_files $uri $uri/ /static-pages/ @proxy;
    }

    location @proxy {
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_pass http://wordpress;

        proxy_set_header      X-Real-IP $remote_addr;
        proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header      Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Reference:

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#proxy-everything

BUT! When I try accessing / I get 403 access denied for /static-pages/index.html (which doesn't exist). How can I tell it NOT to check /index.html and just fall back to the proxy if there's no directory that matches the request?

0

Working solution:

server {
    error_log  /var/log/nginx/error.log debug;
    listen            80;
    server_name ${NGINX_HOST};
    sendfile          on;
    tcp_nodelay       on;
    root              /static-pages;
    index             index.html index.php;
    try_files $uri $uri/ @proxy;

    location @proxy {
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_pass http://wordpress$uri;

        proxy_set_header  X-NginX-Proxy true;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_set_header  Host $http_host;
        proxy_set_header  Accept-Encoding     "";
        proxy_set_header  Proxy               "";

    }

    location = / {
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_pass http://wordpress;

        proxy_set_header  X-NginX-Proxy true;
        proxy_set_header  X-Real-IP $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Forwarded-Proto $scheme;
        proxy_set_header  Host $http_host;
        proxy_set_header  Accept-Encoding     "";
        proxy_set_header  Proxy               "";
    }
}

I'd love it if I could squeeze the 2 similar proxies but no biggie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.