Welcome! Log In Create A New Profile

Advanced

Send all 404s to Apache without changing requested filename

Posted by Zerxer 
Send all 404s to Apache without changing requested filename
June 01, 2009 08:23PM
Hello, we're currently using Nginx to serve all static files and sending the rest of the requests to Apache. I need a way to have Apache handle the request if the requested static file does not exist instead of Nginx still handling it and serving its 404 page.

Someone in the IRC showed me how to make 404s redirect to another location but then that doesn't let Apache know what the actual requested file was in order for our normal .htaccess files to handle the 404 appropriately. How can I make it send the request through Apache still without changing what the requested location was?

Here's a snippet of the current code I'm using from one of the domains (all of them are setup like this): http://pastebin.com/f444e943e


If you can, please alter the above code for how it should be done. Thanks to anyone who can help.
Re: Send all 404s to Apache without changing requested filename
June 01, 2009 09:02PM
If you are running a recent version of nginx which supports "try_files" you can try something like:

[code]
location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|ico|bmp)$ {
root /home/nick/public_html;
try_files $uri @404;
}

location @404 {
proxy_pass 127.0.0.1:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
....
}
[/code]

where Apache is listening on 127.0.0.1:80.

--
Jim Ohlstein
Re: Send all 404s to Apache without changing requested filename
June 01, 2009 09:10PM
Does making the location be named something like @location make it not overwrite the actual location from being the filename? Because the person who showed me the error_page 404 thing actually had something like this:

[code]error_page 404 @fallback

location @fallback {
proxy_pass stuff
}[/code]
Re: Send all 404s to Apache without changing requested filename
June 01, 2009 09:20PM
@404 or @fallback simply references a location. You can use @whatever.

What "try_files" does is it tells nginx to try to serve the listed files in order (in this case your URI as represented by $uri) and if it doesn't find the requested resource then it goes to the location "@...". In the configuration I posted, @404 location is a proxy_pass directive.

--
Jim Ohlstein
Re: Send all 404s to Apache without changing requested filename
June 01, 2009 09:59PM
One more question. Is it possible to make a 'location' directive reference 2? Since "location /" for us already does the proxy_pass stuff, is there a way to make that location line handle both / and @whatever? So we don't have to duplicate all the proxy_pass code? If not, oh well. Thanks for the help so far.
Re: Send all 404s to Apache without changing requested filename
June 01, 2009 10:28PM
I'm not sure what you're asking. The simplest way to find out if your config file is valid is to run

# nginx -t

--
Jim Ohlstein
Re: Send all 404s to Apache without changing requested filename
June 01, 2009 10:29PM
What I was asking was if it's possible to do an "or"/"else" type statement.

Example (fake, obviously):

location / or @404 {
stuff
}

Instead of having two separate location directives.
Re: Send all 404s to Apache without changing requested filename
June 01, 2009 10:31PM
No, I don't think so.

--
Jim Ohlstein
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 134
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready