user2991036
user2991036

Reputation: 421

Block a specific port in Nginx

I have the following config:

server {
    listen 80;

    server_name my_server.com;

    location / {
        proxy_pass http://localhost:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
   }
}

The application is run on port 8080. How can I block this port? I mean when some user opens http://my_server.com:8080 then server sends no response.

Thank you!

Upvotes: 4

Views: 4954

Answers (2)

Dumisani Kunene
Dumisani Kunene

Reputation: 739

It is easier with the uncomplicated firewall if on Debian/Ubuntu. UFW modifies iptables as suggested by @Grigory Kislin.

sudo ufw allow 22   #(Optional) To avoids locking yourself out if using SSH on port 22  
sudo ufw deny in 8080
sudo ufw enable
sudo ufw reload

#To renable the port uncomment and run:
#sudo ufw allow in 8080

Upvotes: 0

Grigory Kislin
Grigory Kislin

Reputation: 18030

It's better do at network level. For unix:

iptables -A INPUT ! -s 127.0.0.1 -p tcp -m tcp --dport 8080 -j DROP

See https://serverfault.com/a/248864

Upvotes: 2

Related Questions