Nginx rewrite omitting port - nginx

I've got a an nginx setup that's proxying all requests to a rails app. I would like to have a some kind of "maintenance-mode", so that if I use a different nginx-config, all incoming requests should return a local maintenance.html page.
Here's what I've come up with:
server {
listen 3000;
server_name localhost;
root html/; #maintenance.html resides here
location / {
# redirect everything not already going to maintenance.html
if ($request_uri !~ ^.*/maintenance.html$ ) {
rewrite ^(.*)$ /maintenance.html break;
}
}
}
With this setup, I am able to directly access http://localhost:3000/maintenance.html. However, if I trigger the rewrite rule, the new URL results in localhost/maintenance.html instead of http://localhost:3000/maintenance.html.
This results in a completely empty page being rendered.
nginx version: nginx/1.0.6 on Windows 7
thx for any help
UPDATE:
I just noticed, it works fine with Internet Explorer and also with Chrome. Only Firefox seems to have that problem.

location / {
try_files /maintenance.html $uri =404;
}

Related

Nginx - config to route to single page app

I'm using nginx with the deployment of my website on my local server.
The website is a single page create react app running on the server. I have a domain, www.test.com for example, and i want the single page app to be found on www.test.com/first-website. From reading online I'm supposed to use the rewrite directive.
This is my current config:
http {
upstream sensory-showcase {
server 127.0.0.1:5000;
}
server {
listen 80;
location /sensory-solution-for-firefighters {
rewrite ^/sensory-solution-for-firefighters $1 break;
proxy_pass http://sensory-showcase/;
}
}
}
events { }
From this the url resolves without an nginx 500 error however it just shows a blank white page.
And i have to have a trailing /, eg www.test.com/first-website/ . Without the trailing / it errors.
I just note, when I didnt have the rewrite directive in, and left the location at just / the site loaded fine.
Try this, it will fallback to the index.html of your single page app, which will do the routing:
location /first-website {
try_files $uri $uri.html $uri/ /first-website/index.html;
}

In Nginx, redirect to another site if img is down

I have a site using some old link pngs, like :
<img src="http://j.cd.top/uploads/jvyue/news/2020/04/10/15864804147273771862.png" style="height:211px; width:330px">
and the png is not present, so I tried to rewrite it to a local server by setting Nginx:
location ~ .*\.(gif|jpg|jpeg|png)$ {
try_files $uri #apache; }
location #apache{
rewrite ^http://j.cd.top/uploads/(.*)$ http://127.0.0.1:8222/$1 permanent;
}
http://127.0.0.1:8222 is local server that server images, but this settings not works, is something wrong?
http://j.cd.top/uploads is not the current website, it is a legacy link.
I also tried proxy pass :
location ~* \.(gif|jpg|jpeg|png)$ {
proxy_pass http://127.0.0.1:8222;
}
Scheme and hostname are not included in the $uri variable which is tested by rewrite directive, try this one:
rewrite ^/uploads/(.*)$ http://127.0.0.1:8222/$1 permanent;

Nginx URL rewrite

I have a website that has laravel setup to run under http://www.example.com/lara/
So, most Laravel pages have URLs of type http://www.example.com/lara/page/23 OR http://www.example.com/lara/category/23 etc.
Nginx is the underlying server and it has the following configuration to handle these requests:
location /lara/ {
try_files $uri $uri/ /lara/index.php?$query_string;
}
Everything works ok.
Now, I need to setup a special page with the URL http://www.example.com/mystuff/ which actually is handled by
http://www.example.com/lara/category/29
To get this working I added the following rewrite right below location /lara/, that is:
rewrite ^/mystuff/(.*)$ /lara/category/29/$1 last;
Unfortunately, I get a page not found error. Any insights?
Further investigation & research:
1)
location /mystuff/ {
return 301 /lara/index.php/category/29;
}
worked although that's not (browser address bar changes to) what I actually want.
2) Looks like Laravel is not seeing the updated REQUEST_URI.
Try this
server {
...
rewrite ^/lara/(.*)\..*$ $1/mystuff last;
return 403;
...
}

Nginx: How to rewrite only when all location blocks fail?

I have a web site which needs to be using https connection for pretty much the whole site except a few locations which I need to be served via http. To do that I have two servers setup in nginx config. One is for non-secure and one for secure connections. However for the non-secure server, I want to be able to rewrite to the secure web one only when none of the location blocks are validated.
Is that possible? If yes, how?
Structure of my nginx config:
server {
listen 80;
...
location /foo1 { ... }
location /foo2 { ... }
# i can't get this rewrite to work only when all location blocks fail
rewrite ^/(.*) https://foo.com/$1 permanent;
}
server {
listen 443;
...
}
Thanx
Add to the end of the server block:
location / {
rewrite ^/(.*) https://foo.com/$1 permanent;
}

nginx multiple domain virtual host configuration

I'm setting up nginx with multiple domain or wildcard support for convenience sake, rather than setting up 50+ different sites-available/* files. Hopefully this is enough to show you what I'm trying to do. Some are static sites, some are dynamic with usually wordpress installed.
If an index.php exists, everything works as expected.
If a file is requested that does not exist (missing.html), a 500 error is given due to the rewrite. The logged error is:
*112 rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/missing.html"
The basic nginx configuration I'm currently using is:
`
listen 80 default;
server _;
...
location / {
root /var/www/$host;
if (-f $request_filename) {
expires max;
break;
}
# problem, what if index.php does not exist?
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
...
`
If an index.php does not exist, and the file also does not exist, I would like it to error 404. Currently, nginx does not support multiple condition if's or nested if so I need a workaround.
if ($_SERVER['HTTP_HOST'] == 'desideshat.com')
{
$vboptions['bburl'] = 'http://www.desideshat.com';
}
else
{
$vboptions['bburl'] = 'http://www.vedesi.com';
}
?>

Resources