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
Related
I have a rather simple question, i have an enviroment, where i have often changing subdomains, but some are also presistent, now i want to forward api.example.com to 1.2.3.4:8080 and all other incoming requests to 2.3.4.5:9090. My setup till now is that i forward all requests from the api subdomain points to 1.2.3.4:8080:
server {
listen 80;
listen [::]:80;
server_name api.example.com;
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;
location / {
proxy_pass http://1.2.3.4:8080;
}
}
Now i need a way to point all other subdomains to 2.3.4.5:9090.
Use default_server in listen. See docs.
server {
listen 80;
listen [::]:80;
server_name api.example.com;
location / {
proxy_pass http://1.2.3.4:8080;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
proxy_pass http://2.3.4.5:9090;
}
}
All you need is to dynamically resolve your subdomains. the following config will take care of your situation.
server {
listen 80;
listen [::]:80;
server_name ~^(?<subdomain>.+)\.example\.com;
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;
location / {
if ($subdomain = "api") {
proxy_pass http://1.2.3.4:8080;
}
proxy_pass http://2.3.4.5:9090;
}
}
I have a wordpress website with https protocol by configuring the nginx 301 redirect:
server {
listen 80;
server_name xxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}
And my article has some image links with static server like:
http://yyy.com/1.png
But when i access this article: it will be https://yyy.com/1.png, How do I configure the nginx that can still use http for the image static server?
You would do that using below config
server {
listen 80;
server_name xxx.com;
location ~* \.(png|ico|jpeg)$ {
root <your root folder>;
try_files $uri =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name xxx.com;
ssl_certificate conf.d/xxx.crt;
ssl_certificate_key conf.d/xxx.key;
}
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/.
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;
}
}
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;
}