Redirect urls in nginx - nginx

I have a problem.
My app server is nginx, on which my blog was hosted.
when i visited my sitemap with this url:
http://www.ikbear.com/sitemap.xml, it works.But when i visited my sitemap with this url:
http://ikbear.com/sitemap.xml, it doesn't work. So i want to redirect http://ikbear.com/sitemap.xml to http://www.ikbear.com/sitemap.xml, would you tell me how can i do that in nginx? Thanks!

Actually I'm going to venture a guess that you'll have the same trouble redirecting that url as actually serving it.
First, here's the syntax for a basic redirect:
server {
# ...
# redirect sitemap.xml to sitemap.xml.php
rewrite ^(/sitemap.xml)$ /sitemap.xml.php;
# ...
}
What might work for you is getting both www and not-www serving correctly. A common strategy is to serve all www to non-www, or vice versa. Here's an example of that:
server {
listen 80;
server_name www.mydomain.com;
# forward everything from www.domain.com to domain.com
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name domain.com *.domain.com;
location / {
root /var/www/domain/htdocs;
index index.html index.htm index.php;
# ... etc.
}
}

Related

Nginx Mess (URL Redirects)

I'm looking to redirect old urls to the new urls / new links. It's proving to be a mess to do just that. I'm using nginx 1.19.6 and ubuntu 18.04
I want to setup my redirects so https://ikhnetworks.com/feeds/Live/* would redirect to https://ikhnetworks.com/feeds/HD1/
https://ikhnetworks.com/Stations/* -> redirect to the respective call letters - for example
https://ikhnetworks.com/Stations/Radio/WDJO/ > https://ikhnetworks.com/WDJO/
For the first part, I can provide you the solution as:
server{
listen 443;
server_name ikhnetworks.com;
...
...
root /var/www/html;
location /feeds/Live {
return 301 https://ikhnetworks.com/feeds/HD1/;
}
}

Nginx subfolder response inconsistent

I have a front-end application (in reactjs) and a static blog that I want to display from the same server through nginx. My application is accessible at the root of the domain / and my blog is currently accessible on a subdomain blog.domain.com. But I'm currently switching from a subdomain to the /blog subfolder on the same domain and I have an issue while displaying the blog from the subfolder. The problem only appears on chrome and is quite inconsistent. What happens is that when browsing /blog I get a 404 from the js application, not the static site (the css styles are different so it's easy to spot who responded). If I refresh the page without cache (ctrl + shift + R) I get a response from static blog. If I click on a link, again I get a 404 from the js page and if I clean-refresh now I get the correct page from the blog. Any f5 on a page redirect to a 404 from the js page.
In firefox or private navigation, everything seems fine though so I was thinking about a common cache issue in the browser at first but the fact that after dropping the cache I still get 404s from the wrong directory makes me think about some parameters that would be sent and not treated the same way in one case or another.
I have noticed that ctrl-shift-R redirects me to /blog/ from /blog when the blog is displayed properly but an f5 on /blog/ still gives a 404 from the js frontend. It might mean that the 301 to the trailing slash does something more. But a ctrl-shift-R on /blog/ does not trigger a 301 (just a 200) with the expected website displayed.
Here is my nginx config file for this site:
server {
listen 443 http2 default_server;
listen [::]:443 http2 default_server;
root /opt/react_folder/build;
index index.html;
server_name domain.com www.domain.com;
location / {
try_files $uri $uri/ /index.html =404;
}
location /blog {
alias /opt/blog_subfolder;
try_files $uri $uri/ /index.html =404;
}
... backend block
... ssl block
}
server {
listen 0.0.0.0:80;
server_name domain.com www.domain.com;
rewrite ^ https://$host$request_uri? permanent;
}

Redirect root to another folder in nginx server

I am very new to nginx. I was using Apache previously and was using htaccess to redirect root to another folder. Now migrated to nginx. Here is four things I want to achieve
Redirect http to https e.g. http://www.example.com -> https://www.example.com
Then redirect root to another folder but URL must be rewritten as example.com not example.com/blog
All files in php should show as html in url e.g. example.com/contact.php -> example.com/contact.html
example.com/page.php?content=file -> example.com/file
I found this code to redirect but don't know where to insert this code nginx.conf or any other file?
server{
location = / {
return 301 https://www.example.com/blog;
}
}
Also please suggest me if these changes are made in nginx.conf file or /etc/nginx/sites-available/example.com file.
To redirect HTTP to HTTPS traffic you can create another server block to match the incoming HTTP and domain then rewrite to your HTTPS server block.
Which file do you put this in? Both /etc/nginx/nginx.conf and /etc/nginx/sites-available/example.com should get read (unless you changed config) so it shouldn't matter, but I personally put these configs in /etc/nginx/sites-available/example.com because I consider it part of the same domain.
file: /etc/nginx/sites-available/example.com
server {
listen 80;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
...
# your location blocks and redirects here
}

Handle index.php and non-www in nginx

I have a nginx config file that currently handles rewriting of requests via index.php for a Zend Framework application. Example code as follows:
server {
listen 80 default_server
etc....
rewrite ^/index\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^/(.*)$ /index.php/$1 last;
}
etc....
}
However, I now need to add a redirect so that any non-www requests for any domain are processed with a 301 to the www equivalent.
I've come across examples such as:
server {
listen 80;
server_name ~^(?!www\.)(?<domain>.+)$;
return 301 $scheme://www.$domain$request_uri;
}
But how would I combine everything so that any non-www are first redirected with a 301 and then rewritten via index.php?
Two separate server blocks (exactly as you have in your question). The first server block is the default, which handles the www prefix domains already, and (presumably) handles the index.php rewrite correctly.
The second server block only matches domain names without the www prefix.
If you wanted to do it the other way around, that is rewrite index.php before you redirect, then just add a rewrite line to the new server block.

How to Redirect a non-www to www with Mutiple Sites? (Nginx and Varnish)

First I want to clarify that I searched a lot about this issue, but I haven't found a solution.
I have 2 Wordpress sites configured under the same Nginx (port 8080) and Varnish (port 80).
This is my actual setting:
map $http_host $blogid {
default 0;
www.site1.com 1;
www.site2.com 2;
}
server {
listen 8080;
server_name www.site1.com site1.com;
root /var/www/site1.com;
...
}
server {
listen 8080;
server_name www.site2.com site2.com;
root /var/www/site2.com;
...
}
What I want to do, is to configure a redirect for site2 from a non-www to a www. Example: http://site2.com -> http://www.site2.com
I added another 'server' configuration with the redirect, and let just 'www.site2.com' in the other 'server_name'.
map $http_host $blogid {
default 0;
www.site1.com 1;
www.site2.com 2;
}
server {
server_name www.site1.com site1.com;
root /var/www/site1.com;
...
}
server {
listen 8080;
server_name site2.com;
return 301 http://www.site2.com$request_uri;
}
server {
server_name www.site2.com;
root /var/www/site2.com;
...
}
After changed with the configuration above and restarting Nginx, what happened is when accessing "http://site2.com" (without www) it is loading the content from "http://site1.com" (url continues site2.com without www). Acessing "http://www.site2.com" shows the right content.
What I'm doing wrong?
I think that redirect is not working because I tried to redirect to Google.com, but it didn't redirect. It keeps loading "site1.com" content inside "site2.com".
return 301 http://www.google.com
I tried this code below, but with the same result:
rewrite ^(.*) http://www.site2.com$1 permanent;
My complete Nginx configuration:
http://codepad.org/TfPHS0jH
Your site isn't being served by Nginx but by Varnish.
When you navigate to any of http://www.siteX.com or http://siteX.com, you are going to http://www.siteX.com:80 or http://siteX.com:80 and according to your explanation, Varnish listens on Port 80, not Nginx.
There is no real reason to ever have to use the two together (despite what you may have read on some websites) and you should get rid or one or the other and focus on the one you decide to keep.
After long time, this was my solution:
http://www.softprayog.in/troubleshooting/how-to-redirect-non-www-urls-to-www-in-varnish

Resources