Skip to content

Instantly share code, notes, and snippets.

@kiranparajuli589
Created March 13, 2023 18:33
Show Gist options
  • Save kiranparajuli589/7f74d3fdf853e6a8d95c93c7076a21a2 to your computer and use it in GitHub Desktop.
Save kiranparajuli589/7f74d3fdf853e6a8d95c93c7076a21a2 to your computer and use it in GitHub Desktop.
Configuration to route a request from domain `sub.xxx.com` to `localhost:8000` in nginx config

To configure Nginx to route a request from a subdomain, sub.xxx.com, to localhost:8000, you can follow these steps:

  1. Open the Nginx configuration file for the domain, usually located at /etc/nginx/sites-available/sub.xxx.com.

  2. Add the following server block to the file:

    server {
        listen 80;
        server_name sub.xxx.com;
    
        location / {
            proxy_pass http://localhost:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
    

    This server block listens on port 80 for requests to sub.xxx.com, and proxies them to localhost:8000 using the proxy_pass directive. The proxy_set_header directives are used to set the necessary headers for the proxy server to forward the request correctly.

  3. Save the changes and exit the configuration file.

  4. Test the Nginx configuration using the following command:

    sudo nginx -t
    

    This command checks the syntax of the configuration file and reports any errors.

  5. If there are no errors, reload the Nginx service using the following command:

    sudo systemctl reload nginx
    

    This command reloads the Nginx configuration with the changes made.

With these steps, Nginx is configured to route requests from the subdomain sub.xxx.com to localhost:8000.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment