Nginx - if pattern 404 - go to - nginx

I have list of urls like:
domain.com/some-url-key-with-possible-id-after-it-99999.html
I need to try that URL, and IF it returns 404, redirect to:
domain.com/some-url-key-with-possible-id-after-it.html
Is that possible?
location ~ /([a-zA-Z0-9\-]+)-([0-9]+).html$ {
## IF ABOVE IS 404
return 301 http://domain.com/$1.html;
## ENDIF
}
I found something like this:
server {
listen 12440;
root /some/path/here/nginx/html/noahc/;
server_name www.domain.net, domain.net;
port_in_redirect off;
location /{
error_page 404 = #foobar;
}
location #foobar {
rewrite .* / permanent;
}
}
But it doesn't satisfy me, because I need to redirect to url with variable from request pattern. It could be ok, if I'll be able to pass ([a-zA-Z0-9-]+)-([0-9]+) to it as an argument.

So you have a URI and you would like to rewrite it if the static file does not exist. Use try_files to test for file existence.
root /path/to/docroot;
location ~ ^(/[a-zA-Z0-9-]+)-[0-9]+\.html$ {
try_files $uri #rewrite;
}
location #rewrite {
return 301 $1.html;
}
You can use return 301 or rewrite ... last in the named location, depending on how visible you want the rewrite to be.
See this document for help with nginx directives.

Related

Nginx config for hosting two nested websites

I am trying to write a Nginx config so that:
If the URL is exactly /Site/ it must serve C:\Site\index.html
If the URL is /Site/something/else it must serve C:\Site\something\else (fallback to index.html if it doesn't exists) BUT only if /something/ is different from /api/ and /dev/
If the URL is /Site/api/something/else it must redirect the exact request to another server otherserver/api/something/else
If the URL is exactly /Site/dev/ it must serve C:\Site-Dev\index.html
If the URL is /Site/dev/something/else it must serve C:\Site-Dev\something\else (fallback to index.html if it doesn't exists) BUT only if /something/ is different from /api/
If the URL is /Site/dev/api/something/else it must redirect the exact request to another server otherserverdev/api/something/else
So the key point here is that:
/Site/ and /Site/dev are two different websites
Each one has its own .../api endpoint
So far I wrote this config:
server {
listen 19001;
include mime.types;
location ^~ /Site/api/ {
proxy_pass http://otherserver;
}
location ^~ /Site/dev/api/ {
proxy_pass http://otherserverdev;
}
location = /Site/ {
root C:\Site;
try_files /index.html =404;
}
location ~ ^/Site/(.*) {
root C:\Site;
try_files $1 $1/ /index.html =404;
}
location = /Site/dev/ {
root C:\Site-Dev;
try_files /index.html =404;
}
location ~ ^/Site/dev/(.*) {
root C:\Site-Dev;
try_files $1 $1/ /index.html =404;
}
}
This doesn't work at all, I'm getting index.html in every request and also dev and nondev are mixed up.
Is there a way to achieve what I need?

Condition, if rewrite return 404 status, return another page

I have a rewrite for old unused subdomains 90% URL corresponds to both the primary and subdomain, but some url are unique to the subdomain.
How to make a condition, if the rewrite returns 404, then return a custom page.
if ($host ~* (sub1|sub2|sub3)\.mysite\.loc) {
rewrite ^(.+)$ http://mysite.loc$1 permanent;
location / {
error_page 404 =301 http://mysite.loc/lost-sub;
}
}
You could use try_files, for example:
location / {
try_files $uri #failover;
}
location #failover {
proxy_pass http://failover;
}
In this case, if file not found instead of returning a 404 it will try the location #failover

How to use my index.htm in this location script?

I need URL http://myexample.org (root) redirecting to my local index.htm, not rewriting it to Github... How to do it?
I was testing many variations of location = / { try_files ...} but no one works. Using a UBUNTU 16 LTS server.
/etc/nginx/sites-available/myexample.org
server {
server_name myexample.org;
root /var/www/myexample.org;
index index.html index.htm;
# ?? location =/ {...} is not working!
location / {
# also not work a root rewrite
# rewrite ^/?$ index.htm break;
rewrite ^/?git$
http://github.com/myexample-org/test
break;
rewrite ^/?tickets$
http://github.com/myexample-org/test/issues
break;
rewrite ^/?(.+)$
http://github.com/myexample-org/test/$1
break;
}
}
Change your last rewrite directive to match ^/(.+)$
location = / will not work for an index, as the rewritten URI will match location / which will then hit your final rewrite statement.
Your original solution (a few questions ago) with the named location, should work fine:
root /path/to/file;
index index.htm;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/... http://example.com/...;
rewrite ^/... http://example.com/...;
rewrite ^/... http://example.com/...;
}
Assuming that a file called /path/to/file/index.htm exists on this server. The break flag is unnecessary as the destination URL begins with http://. If you want to add a flag, the redirect or permanent flag would be pertinent. See this document for details.

Rewrite directory as parameter

I'm trying to redirect requests from
example.com/abc234 to
example.com/setup.html?s=abc234
So far, I've tried the following, but it seems to always end up either 1) not transmitting the parameter or 2) ending up in an infinite loop (or 404) because it also tries to redirect the redirected request? The request has to be visibly rewritten because I want to pick up the parameter with JS, not PHP.
server {
server_name example.com;
root /var/www/html;
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}
I've also tried various combinations of location / { try_files ...; } or using the absolute URL within rewrite without success.
One technique is to rewrite only URIs that do not match a physical file.
For example:
server {
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
return 302 /setup.html?s=$uri;
}
}
See this document for more.
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}

Nginx 301 Redirect all non home page urls to home page

I'm shutting down a site and I need to 301 redirect all pages to the home page where I have a message saying that the site is being closed down.
Basically any http://example.com/anyfolder -> 301 to http://www.example.com
I have the following but this results in a redirection loop.
location ~ ^/([A-z]+) { rewrite ^ / permanent; }
What is the proper way to do this in nginx?
Make it simple.
location / {
rewrite ^ http://example.com;
}
location = / {
}
This worked for me. This is assuming you only have a index.html,htm and the other urls are missing the physical file on disk.
server {
listen 80;
server_name www.example.com example.com;
root /u/apps/example/www;
index index.html;
location / {
if (!-e $request_filename) {
rewrite ^ / permanent;
}
}
}
The below is a more modern syntax for trying various file resources for existence, with progressive defaults.
Be sure to uncomment other location paths underneath the one below, to avoid more specific matches, if necessary.
Also, if is evil when used in location context.
server {
listen 1.2.3.4:80;
server_name example.com;
root /path/to/document/root;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}

Resources