Reputation: 45
I am new to nginx and just trying to do something which I believe should be simple. If I do:-
curl http://localhost:8008/12345678
I would expect the index.html page to be returned. But instead I get 404 not found. /usr/share/nginx/html/12345678 no such file
If I do curl http://localhost:8008/ I expect the request to be routed to http://someotherplace/ but instead I get 302 found and thats it.
Apologies for the basic questions but would be grateful for some advice!
This is the code:
server {
listen 8008;
server_name default_server;
location / {
rewrite ^/$ http://someotherplace/ redirect;
}
location ~ "^/[\d]{8}" {
rewrite ^/$ /index.html;
root /usr/share/nginx/html;
}
}
Upvotes: 3
Views: 2258
Reputation: 14269
Please try this
server {
listen 8008;
server_name default_server;
root /usr/share/nginx/html;
location / {
proxy_pass http://someotherplace/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location ~ "^/[\d]{8}" {
rewrite ^(.*)$ /index.html break;
}
}
The proxy_pass
will route the request to your remote destination and return the response.
Instead of rewrite
you can use try_files
as described by Richard Smith.
Upvotes: 1
Reputation: 49812
The ^/$
does not match the URI /12345678
- it only matches the URI /
.
You could use:
rewrite ^ /index.html break;
The ^
is just one of many regular expressions that match anything. The break
suffix causes the rewritten URI to be processed within the same location
block. See this document for details.
You can achieve the same result using the try_files
directive:
location ~ "^/[\d]{8}" {
root /usr/share/nginx/html;
try_files /index.html =404;
}
The =404
clause is never reached as index.html
always exists - but try_files
must have at least two parameters. See this document for details.
Upvotes: 3