A
A
Aleksandr2018-08-10 14:52:47
Nginx
Aleksandr, 2018-08-10 14:52:47

How to make a redirect using nginx from IP to the site's domain name?

Essence of the question: now when I try to enter the server's IP in the browser, I get an error that the ssl certificate is not valid (everything is logical).
To remedy the situation, I want to send everyone who tries to access the site by IP to the site by domain name.
To do this, I added a config file for the IP address to /etc/nginx/sites-enabled/.
Please help me find the error and make the redirect correctly.
Content:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name server_ip_address ; # www and your ip address
ssl_certificate /etc/letsencrypt/live/servername/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/servername/privkey.pem; # managed by Certbot
return 301 https://server_domain_name$request_uri;
}
# HTTPS - proxy all requests to the Node app
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name server_ip_address ;
# Use the Let's Encrypt certificates
ssl_certificate /etc/letsencrypt/live/servername/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/servername/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
return 301 https://server_domain_name$request_uri;
# Include the SSL configuration from cipherli.st
#include snippets/ssl-params.conf;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass localhost:5000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil, 2018-08-10
@Veneomin

Have you looked at this option?

A
Anatoly, 2018-08-10
@trofimovdev

1. Listening on port 80, you want to connect SSL. Well, that's it.
2. Listening on port 443 of the IP address, you, again, want to enable SSL. As far as I know, SSL does not apply to IP addresses.

listen 80;
server_name server_ip_adress;
return 301 https://server_domain_name$request_uri;

And already in the domain config connect certificates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question