Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a single nginx virtual server deal with both port 80 and 443?

Tags:

nginx

ssl

I have a config file with a virtual server setup, this is running on port 443 for ssl. I would also like this same virtual server to handle non ssl traffic on port 80.

I was hoping to do the following but it doesn't seem to work.

server {     listen 443 ssl;     listen 80;     server_name example.com;     ... } 

It looks like the ssl options below these settings are causing problems for the non ssl traffic.

like image 858
ArthurGuy Avatar asked Aug 20 '14 08:08

ArthurGuy


People also ask

Can Nginx listen on two ports?

To make Nginx Listen on multiple ports for a single virtual host file, you can add multiple listen directives. If you want to make Nginx listen for different virtual hosts on different ports, you can use different ports in listen directive in different virtual host files. It's that easy!

How do I get Nginx to listen on port 443?

Setting up an HTTPS Server. To set up an HTTPS server, in your nginx. conf file include the ssl parameter to the listen directive in the server block, then specify the locations of the server certificate and private key files: server { listen 443 ssl; server_name www.example.com; ssl_certificate www.

How do I enable HTTP and HTTPS in Nginx?

Turns out it is possible to have both HTTP and HTTPS servers and choose HTTPS by default. I just had to mix the two servers of the conf file in one, just like: server { listen 80; listen 443 default_server ssl; ssl_certificate certificate. crt; ssl_certificate_key server.


2 Answers

Yes, of course.

server {   listen 80;   listen 443 ssl;   # force https-redirects   if ($scheme = http) {     return 301 https://$server_name$request_uri;   } } 

Here is my post, name "Nginx Configuration for HTTPS" which contains more info.

like image 146
Haimei Avatar answered Oct 02 '22 09:10

Haimei


Remove ssl on; directive.

ssl flag in listen directive is exactly what you need.

See http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

like image 28
Alexey Ten Avatar answered Oct 02 '22 09:10

Alexey Ten