Rewrite module NGINX - nginx

Hi how can I rewrite my URL into a certain format?
Example:
# I have this URL
http://www.mywebsite.com
I would like it to be written into something like this
https://www.mywebsite.com.truesite.com
So if my URL is something like this
http://www.mywebsite.com/newsite?test=message
# it will be rewritten into like this
https://www.mywebsite.com.truesite.com/newsite?test=message
I'm not really good when it comes to regex so I have no idea on how to accomplish this on NGINX

Since this is an HTTP to HTTPS passing, you need more than the rewrite module of Nginx.
server {
listen 80;
listen [::]:80;
server_name www.mywebsite.com;
return 301 https://www.mywebsite.com.truesite.com$request_uri;
}
You need to have another block on your https website which includes SSL.

Related

Nginx URL forwarder to another domain

How can I redirect someone from one domain to another whilst keeping the current URL path?
For example:
example.com/ABC -> example2.com/file.html?x=ABC
You can create a rewrite rule to forward from the old domain to the new one.
server {
listen 80;
server_name example.com;
return 301 http://new.example.com$request_uri;
}
Documentation: http://nginx.org/en/docs/http/converting_rewrite_rules.html

Fourth-Level Subdomain Forwarding

I've recently been trying to set up a reverse proxy that would forward certain 4th-level subdomains to particular locations. So, for example, this is what I'm trying to accomplish (configuration in my nginx file):
server {
listen 80;
server_name *.server.domain.com;
rewrite ^ https://$server_name$request_uri;
}
The goal here being that if someone went to, for example, http://item1.server.domain.com, they would be re-routed to https://item1.server.domain.com. However, with this configuration, the URL gets rewritten to https://%2A.server.domain.com.
Is there a way to fix this so that the full domain (item1) gets added correctly to the rewritten URL? Ideally, I wanted it to eventually be able to rewrite any subdomain on server.domain.com directly to https.
Thanks!
The $server_name variable contains the text from the value of the server_name directive. The %2A is a URL encoded representation of the leading *.
Use $host or $http_host to obtain the hostname actually requested by the client. See this document for more.
For example:
server {
listen 80;
server_name *.server.domain.com;
return 301 https://$host$request_uri;
}
Note: Restart nginx and clear the browser cache between each test. Check the configuration using nginx -T.

nginx rewrite for subsubdomains to subdomains

We have an application where we use subdomains for each of our customer's installations. so we have customer1.ourapp.com, customer2.ourapp.com, customer3.ourapp.com and so on.
Because of security we want to redirect all http to https, since we have a wildcard SSL certificate.
Also, some customers are not that tech savvy and add www to their domain name, so then you get things like: http://www.customer1.ourapp.com or https://www.customer1.ourapp.com. In those cases the SSL certificate isn't valid because of the subsubdomain.
I'm trying to write the vhost config for nginx to make the correct redirect in these two cases. I got the http to https redirect to work with:
server {
listen 80;
server_name *.ourapp.com;
#Rewrite all nonssl requests to ssl.
return 301 https://$host$request_uri$is_args$args;
}
correct url's use:
server {
listen 443;
server_name *.ourapp.com;
#Rest of config
}
Made an attempt for the subsub domains, but it's not matching:
server {
server_name "~^(.*)\.(.*)\.ourapp\.com$";
return 301 https://$2.ourapp.com$request_uri;
}
Any idea how to get this working?
Wildcarded server takes precedence over regexp'ed one and matches 'www...' too.
You can use one definition for both cases:
server_name ~ ^(?:.*\.)?(.*)\.ourapp\.com$;
return 301 https://$1.ourapp.com$request_uri;

Nginx subdomains with www

I have Nginx configured in a Amazon EC2 server.
Right now both www.myserver.com, .myserver.com works perfect.
What I need, is configure www..myserver.com. I need to redirect the user to .myserver.com. I mean, I need to rewrite the url or something.
How can I do that?
server {
listen 80;
server_name www.myserver.com;
return 301 http://myserver.com$request_uri;
}
You don't need "rewrites" in nginx. Leave them to apache, and read the docs:
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/request_processing.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html
Upd:
That is not clear from your question (who knows, what do you mean by double dots). But if you have ever tried to read the docs, you would easily modify the example for your needs:
server {
listen 80;
server_name ~^www\.([^.]+\.myserver\.com)$;
return 301 http://$1$request_uri;
}

NGINX rewrite rule to remove leading www for Vanity URL

I've searched everywhere and although there are 1000s of examples of how to strip a leading www from a URL using NGINX rewrite rules, I've yet to find an example of how to strip the leading 'www' from a vanity url.
For example, convert 'www.fred.mysite.com' to 'fred.mysite.com'
Can you share an example of how this should work in an nginx rewrite rule?
the easiest way to do it is with a 2nd serverblock as follows:
server {
listen [::]:80; listen 80;
server_name www.fred.mysite.com;
return 301 $scheme://fred.mysite.com$request_uri;
}
server {
listen [::]:80; listen 80;
server_name fred.mysite.com;
#your site setup goes here
}
though you probably want to use "server_name *.fred.mysite.com;" in the 1st serverblock just to catch every possible extra prefix including misspellings

Resources