what's wrong with my nginx configuration

Posted December 18, 2018 2.1k views

I have two servers:

server 1 - - have nginx and apache2 installed
apache listen port 8000
nginx listen port 80
nginx is a load balancer and TLS termination

server 2 - - apache listen port 80

https is working perfectly.

the problem is : if the traffic is direct to server 1 then
1. did not redirect to https
2. expose port 8000, when I expected it hidden to the world

I’m new to nginx, how do I improve my configuration beblow:


upstream testpool {

server {


    listen 80 default_server;
    listen [::]:80 default_server;

    #redirect non http to https
    #return 404; # managed by Certbot
    #not recomended rewrite ^ https://$host$request_uri;
    return 301 https://$host$request_uri;


server {

    listen 443 ssl default_server;

    #ssl section, listen 443 is new way of ssl on;
    ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {

    proxy_pass http://testpool;
    include /etc/nginx/proxy_params;
    #return 301 https://$server_name$request_uri;

edited by kamaln7

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hey friend,

I would propose that you are mostly doing this correctly. You could redirect port 8000 to 443, but you want to hide 8000 anyway so I’d just block it in the firewall like this:

iptables -I INPUT -p tcp --dport 8000 -j ACCEPT