Score:0

Nginx Reverse proxy excluding files

eg flag

I've a landing page done using wordpress and is hosted on example.com. I've an app running on app.example.com on external url. When user try to access wordpress files, it should be served from example.com and if that url or folder is not available the url must be masked and must go to remote url as example.com/$1. I've tried using nginx reverse-proxy but it is not working.

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

location @proxy {
    proxy_pass https://example.com/$1;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Michael Hampton avatar
cz flag
What is not working?
eg flag
If the files are not found it must go to proxy. currently it is going to 404 of wordpress. For example if I've searched example.com/test then the files need to be served from external domain google.com but url must be shown as example.com/test.
Score:0
us flag

The try_files only checks if actual files exist on filesystem. Once it founds a file that exists, it uses that one to serve a response. try_files does not act on any HTTP status codes.

Since you always have /index.php in your filesystem, the @proxy target is never used.

You need to implement the functionality by hooking into WordPress 404 error generation hook and perform the proxying there.

eg flag
Thank you. Since I'm not well versed with Wordpress, I'm trying the luck with nginx. ``location / { try_files $uri $uri/ @proxy; } location @proxy { proxy_pass https://app.example.com/$1; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_intercept_errors on; recursive_error_pages on; error_page 404 = @wordpress; } location @wordpress { rewrite ^/(.*)$ /index.php?$query_string break; }``
us flag
Please don't add configuration snippets to comments, they cannot be read properly.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.