I can't figure this out. Can you help?
This is my setup:
Single website on server.
Going to http://... worked fine until I added the https://... settings to my site config.
Going to https://... now works fine.
Going to http://... now just times out.
server {
listen 80;
server_name mywebsite.io www.mywebsite.io;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mywebsite.io www.mywebsite.io;
root /var/www/mywebsite.io/public_html;
index index.html index.htm;
ssl on;
ssl_certificate /etc/ssl/certs/cert_chain.crt;
ssl_certificate_key /etc/ssl/certs/mywebsite.key;
}
Am I doing something stupid?
Cheers
Try:
server {
listen 80;
server_name mywebsite.io www.mywebsite.io;
return 301 https://$host$request_uri;
}
As $server_name is ambiguous when you have more than one.
Related
I was trying to redirect non secure (domain.com and www.domain.com) to secure version and I was getting a "too many redirects" error.
So, I decided to simplify the config to test and try to find out the error.
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
return 302 https://www.google.com;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/cert.key;
server_name example.com www.example.com;
return 302 https://www.amazon.com;
}
If I am not wrong, when I visit http://example.com/ or http://www.example.com/, I should be redirected to https://www.google.com
And I if I visit https://example.com/ or https://www.example.com/, I should be redirected to https://www.amazon.com
But, any case, I am always redirected to https://www.google.com. What is wrong?
Your browser might be caching the redirects.
Try using Incognito windows for both the test cases. Your config file seems to be fine.
For your domain.com, you can have following configuration:
server {
server_name _;
listen 443;
root /var/www/html;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}
my current setup of webpage is:
forum.xyz.pl
I need xyz.pl redirect to forum.xyz.pl
current nginx.conf:
nodebb.conf
I am using aws route53, not sure what value should I put there for root domain also.
thanks
pl to forum.xyz.pl you can simply do:
server {
server_name xyz.pl;
rewrite ^ forum.xyz.pl$request_uri? permanent;
}
This should solve your problem, let me know if you have any other problems. I don't really understand the problem with Route 53 since it is just handling the DNS entries.
I'd do it like this
server {
listen 80;
server_name xyz.pl;
return 301 https://forum.xyz.pl/;
}
server {
listen 80;
server_name forum.xyz.pl;
#Force Https
return 301 https://$host$request_uri;
}
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
#listen [::]:80;
#listen 80;
server_name forum.xyz.pl;
##rest of config goes here
}
Hy everyone,
I have a little problem with nginx server block and I hope that someone from here will know the solution to it.
This is how my configuration looks like:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com;
}
server{
listen 80;
server_name *.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name .example.com;
...
}
So the first block is normal http redirecting to https second is catching subdomains and third is where location blocks are etc.
My problem is that when the second block catches the request would like to add /admin to URL so it would be subdomain.example.com/admin but i have to check if it already has /admin so i don't get something like this subdomain.example.com/admin/admin.
I tried server_name *.example.com$ and server_name ~.example.com(=<id>.*) then if ($id = '') and hundreds of combinations and didn't get anything usefull.
Did anyone here had similar problem and solved it?
server {
listen 80;
server_name *.example.com;
if ($request_uri !~ ^/admin/) {
return 301 https://$host/admin$request_uri;
}
return 301 https://$host$request_uri;
}
I own a website, like example.com by HTTP. Considering the secure stuff, now I want to change the HTTP to HTTPS. And I hope all the old customers could still be able to visit my website even they use example.com which will be redirect to https via Nginx.
Of course, I googled a lot, then my solution is:
upstream www {
server 127.0.0.1:4000;
}
server {
listen 80;
listen 443 ssl;
server_name localhost www example.com;
ssl on;
ssl_certificate /usr/local/etc/docs/example.crt;
ssl_certificate_key /usr/local/etc/docs/example.key;
if ($ssl_protocol = "") {
rewrite ^ https://$host$request_uri? permanent;
}
# below are some other stuff
# ...
}
But when I visit example.com, I got:
400 Bad Request The plain HTTP request was sent to HTTPS port
Then I change the nginx.conf, after reading Redirect in nginx , and config the error_page by 497:
upstream www {
server 127.0.0.1:4000;
}
server {
listen 80;
listen 443 ssl;
server_name localhost www example.com;
ssl on;
ssl_certificate /usr/local/etc/docs/example.crt;
ssl_certificate_key /usr/local/etc/docs/example.key;
error_page 497 https://$host$request_uri;
# below are some other stuff
# ...
}
Then it works, everything is fine. But I just don't know why and the solution of error_page just seems werid. So
after reading Dealing with nginx 400 “The plain HTTP request was sent to HTTPS port” error, I add the default and remove the ssl on.
upstream www {
server 127.0.0.1:4000;
}
server {
listen 80;
listen 443 default ssl;
server_name localhost www example.com;
ssl on;
ssl_certificate /usr/local/etc/docs/example.crt;
ssl_certificate_key /usr/local/etc/docs/example.key;
if ($ssl_protocol = "") {
rewrite ^ https://$host$request_uri? permanent;
}
# below are some other stuff
# ...
}
Great! It works again. But I am not for sure:
Which solution is correct?
If both correct, which is more friendly for SEO?
Solution 1st is really wired, from http://moz.com/learn/seo/redirection, can find that permanent redirection is more friendly.
server {
listen 80;
server_name www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 default ssl;
server_name www example.com;
ssl on;
ssl_certificate /usr/local/etc/docs/example.crt;
ssl_certificate_key /usr/local/etc/docs/example.key;
# below are some other stuff
# ...
}
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;
}