Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Today I set up a self-signed SSL certificate for my Wordpress sites via my Cent OS Nginx VPS. Because I want to use HTTPS only for Wordpress backend.

This is my vhost nginx configuration file (file in here). After I reload the nginx, now this site only works HTTPS. HTTP display Unable to connect error.

Then I run a telnet command via SSH, then it shows following output.

root@host [~]# telnet mysitename.com 80
Trying 199.1xxx.xxx.22...
telnet: connect to address 199.1xxx.xxx.22: Connection refused

I didn't block the port 80. If I disabled CSF, nothing happens. If I removed following codes in sites-enabled config file both HTTP and HTTPS are working. (I don't know how much it's secure and correct)

    listen   199.1xxx.xxx.22:443 ssl;

    ssl on;  
    ssl_certificate /opt/pifpm/ssl/server.crt;
    ssl_certificate_key /opt/pifpm/ssl/server.key;

Could someone please tell me how do I use both HTTP and HTTPS in this virtual host? Is it secure removed above SSL certificate codes from my site's nginx configuration file and use HTTPS (because SSL working without that settings codes)?

Thanks!

share|improve this question

2 Answers 2

Do you have also listen 199.1xx.xxx.22:80; in the server block?

You should remove ssl on; directive. That makes the whole server block as an SSL block, which is not what you want. The ssl directive on the current listen line is enough.

share|improve this answer
    
Yes, I added listen 199.1xx.xxx.22:80; as well as listen 199.1xx.xxx.22:443; which is mentioned in Nginx doc in here for use single server to handles both HTTP and HTTPS requests. Thanks I'll remove ssl on; directive and let you know it success. –  chris Mar 20 at 2:41
    
Update, After I removed listen 199.1xx.xxx.22:80; and use only listen 199.1xx.xxx.22:443;nothing happened. It only works HTTPS. Non SSL giving me Unable to connect –  chris Mar 20 at 2:55

I managed to fix this issue by myself.

When I try to restart the nginx, I can see following warning.

root@host [~]# sudo service nginx restart
Stopping nginx:                                            [FAILED]
Starting nginx: nginx: [emerg] bind() to 199.1xx.xxx.22:443 failed (98: Address already in use)
nginx: [emerg] bind() to 199.1xxx.xxx.22:443 failed (98: Address already in use)
nginx: [emerg] bind() to 199.1xx.xxx.22:443 failed (98: Address already in use)
nginx: [emerg] bind() to 199.1xx.xxx.22:443 failed (98: Address already in use)
nginx: [emerg] bind() to 199.1xx.xxx.22:443 failed (98: Address already in use)
nginx: [emerg] still could not bind()
                                                           [FAILED]

Then, in SSH i run sudo fuser -k 443/tcp and then restart the nginx using service nginx start. Then everything works fine. However, every VPS reboot, I again face HTTP not working issue.

I use the cpXstack plugin to install Nginx in WHM. If this plugin changed the Apache HTTP port 80, it doesn't change the Apache port 443. Therefore, I manually changed Apache port 443 to 1443 and restart the Apache. Now this issue completely fixed and both HTTP and HTTPS working in every VPS reboot.

Don't know what's the reason if none of the VPS services doesn't use port 80, HTTP not working.

share|improve this answer
    
You should remove Apache if you don't use it. –  Tero Kilkanen Mar 20 at 10:04
    
This is WHM cPanel server. So it'll not cause any issue? –  chris Mar 20 at 15:05
    
Unfortunately I don't know about WHM / cPanel, so I cannot help with that. Doesn't WHM cPanel have any mechanism to replace Apache with nginx? –  Tero Kilkanen Mar 20 at 23:44
    
No, WHM official support Apache. But we can install Nginx separately, like I do. Anyway thanks Tero your support. –  chris Mar 21 at 16:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.