Skip to content

Techeons

Imagine | Explore | Innovate

Menu
  • Home
Menu

Nginx: How to redirect the traffic to another port in Nginx

Posted on September 22, 2024

NGINX config to redirect to another port

NGINX Configuration Structure:

  1. Main Configuration File: /etc/nginx/nginx.conf (location may vary)
  2. Virtual Host Configuration Files: /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/ (location may vary)
  3. Snippets: Reusable configuration segments (e.g., /etc/nginx/snippets/)

Basic HTTP config

server {
    server_name example.com;
    listen 80;
    
    # Permanent redirect to a different port
    return 301 http://$host:8080$request_uri;

}

HTTPS config

server {
    server_name example.com;  # Replace with your domain

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    # Permanent redirect to a different port
    return 301 http://$host:8080$request_uri;

}
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    server_name example.com;
    listen 80;
    return 404; # managed by Certbot

}
Share on Social Media
x facebook pinterest linkedin tumblr reddit emailwhatsapptelegrammastodon

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Nginx: How to increase timeout for Nginx
  • Cheat Sheet: Essential Git Commands
  • Setting a default shell in Linux
  • Setting up Composer on Linux
  • Switch easily between Python versions on a Mac using pyenv

Tags

ai alerting aws b2 backblaze certificate cheatsheet cloud commands data-science datalake devops dns docker dremio git gitlab infra jenkins kubernetes linux metabase minikube minio monitoring mount mysql nginx nodejs notebooks openssh php python scala secrets spark ssh ssl ubuntu ufw usb web dev tools windows xampp zeppelin

©2026 Techeons | Design: Newspaperly WordPress Theme