I am attempting to redirect an entire vanity domain to a specific URL for our main domain at the NGINX level. I have tried all of the following, but in all cases the domain will redirect not to the specific page but to the top level domain.
server {
listen 80;
listen 443;
server_name vanityurl.com www.vanityurl.com;
return 301 https://myrealurl.com/internal/landingpage/;
}
server {
listen 80;
listen 443;
server_name vanityurl.com www.vanityurl.com;
location / {
return 301 https://myrealurl.com/internal/landingpage/;
}
}
server {
listen 80;
listen 443;
server_name vanityurl.com www.vanityurl.com;
rewrite ^/$ https://myrealurl.com/internal/landingpage/ permanent;
rewrite ^/(.*)$ https://myrealurl.com/internal/landingpage/ permanent;
}
I've tried each of these individually and all did the same thing, redirect me to https://myrealurl.com/ dropping the internal/landingpage/.
Related
In my nginx server , I want to do this:
mydomain.com/api/xxxx
redirect to mynewdomain.com/api/testing/xxxx
I try to do by different ways but no ones works for me, always return a 502 error;
First way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
return 301 https://mynewdomain.com/api/testing/$uri;
}
}
Second way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/(.*) {
proxy_pass https://mynewdomain.com/api/testing/$args_v;
}
}
And I tryed to only redirect to specific direction and this works, but i don't want to hardcode all petitions to redirect,
Static redirect
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
proxy_pass https://mynewdomain.com/api/testing/redirect1;
}
Any help for make this dynamic?
location ~ ^/api/(.*) {
return 301 https://mynewdomain.com/api/testing/$1;
}
Try this, because $uri also includes /api.
I've changed my conf file so that when a user types in the domain without www it redirects to the domain with www:
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
I also wish for my https for anything under /user
I get the error of too may redirects, where am I going wrong?
So I have:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/example.com/site;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /user {
rewrite ^ https://$http_host$request_uri? permanent;
}
}
For port 443:
server {
listen 443;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
root /var/www/example.com/site;
index index.html index.htm;
ssl on;
ssl_certificate //path here
ssl_certificate_key //path here
location / {
rewrite ^ http://$http_host$request_uri? permanent;
}
location /user {
}
}
With
listen 80 default_server;
you are telling nginx that this server block is the default server for all http requests regardless of the server name.
The directive
return 301 $scheme://www.example.com$request_uri;
sets nginx to redirect all incoming traffic for this server block to www.example.com. That redirected traffic hits the same server block again (default server) and the process repeats itself, hence a redirect loop.
To fix this change your config file and add a second server block:
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name www.example.com;
#rest of your config
[...]
}
server {
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
Port 80 can be left out, as it is the default, in case you are wondering.
The same principle applies to traffic for 443 (in the redirect block you however must give the port and ssl specific configuration).
The idea is to take incoming requests to http://abc.example.com/... and rewrite them to http://example.com/abc/...
That's easy enough to do with a 301/302 redirect:
# rewrite via 301 Moved Permanently
server {
listen 80;
server_name abc.example.com;
rewrite ^ $scheme://example.com/abc$request_uri permanent;
}
The trick is to do this URL change transparently to the client when abc.example.com and example.com point at the same Nginx instance.
Put differently, can Nginx serve the contents from example.com/abc/... when abc.example.com/... is requested and without another client round trip?
Starting Point Config
Nginx config that accomplishes the task with a 301:
# abc.example.com
server {
listen 80;
server_name abc.example.com;
rewrite ^ $scheme://example.com/abc$request_uri permanent;
}
# example.com
server {
listen 80;
server_name example.com;
location / {
# ...
}
}
# abc.example.com
server {
listen 80;
server_name abc.example.com;
location / {
proxy_pass http://127.0.0.1/abc$request_uri;
proxy_set_header Host example.com;
}
}
Probably yet another nginx redirect question... couldn't find the answer, so:
How do I redirect http://domain.com to http://www.domain.com but do not rewrite any subdomain http://*.domain.com given the cloudfoundry nginx configuration which contains this server definition:
server {
listen 80;
server_name _;
server_name_in_redirect off;
}
I tried this configuration
server {
server_name domain.com
rewrite ^(.*) http://www.domain.com$1 permanent;
}
server {
listen 80;
server_name _;
server_name_in_redirect off;
}
but am getting infinite redirects.
Try replacing
server_name _;
with
server_name *.domain.com;
server {
listen 80;
server_name domain.com
return 301 http://www.domain.com$request_uri;
}
server {
listen 80 default_server;
...
}
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/converting_rewrite_rules.html
How can i redireect from https to http?
i have the code below but it does not seem to work.
server {
listen 443;
server_name example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
The answer above will work, you need to generate a self signed cert (or have a real one) and configure nginx as such:
server {
listen *:443;
ssl on;
server_name domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
ssl_certificate /data/certs/domain.crt;
ssl_certificate_key /data/certs/domain.key;
}
Keep in mind, if it is a self signed cert the browser will give you an ugly warning.
Building off jberger's comment a configuration that should work would be:
server {
listen *:80;
server_name example.com;
}
server {
listen *:443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.cert;
ssl_certificate_key /etc/ssl/private/example.com.key;
return 301 http://$server_name$request_uri;
}
if ($host = 'foo.com') {
rewrite ^/(.*)$ http://www.foo.com$1 permanent;
}
You need to create 2 separate server blocks:
Port 443 (HTTPS) - Define everything like PHP, 404, home, root etc in this block. Even if you want to redirect https://www.example.com to https://example.com or vice-versa, do it over here as #coulix has done.
Port 80 (HTTP) - In here you will just use:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# Redirect HTTP to HTTPS
return 301 https://example.com$request_uri;
}