PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How to setup Nginx Config File for a proxy for my docker container and letsencrypt?

 June 25, 2022     docker, lets-encrypt, nginx, reverse-proxy     No comments   

Issue

I have a Ubuntu server running Nginx and Docker. My docker container is running on port 4200.

docker run -d -p4200:4200 my-app:latest

I can verify that it is running

CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                            NAMES
f5be8856b9e2        my-app:latest   "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        80/tcp, 0.0.0.0:4200->4200/tcp   hopeful_diffie

I have setup up my Nginx default config file like so:

server {

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name my-app.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://localhost:4200/;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my-app.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-app.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

}

server {

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name www.my-app.com; # managed by Certbot


    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://localhost:4200/;
    }

    listen [::]:443 ssl ; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my-app.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my-app.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

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


    listen 80 default_server;
    listen [:

:]:80 default_server;

    server_name my-app.com;
    return 404; # managed by Certbot


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


    listen 80 ;
    listen [::]:80 ;
    server_name www.my-app.com;
    return 404; # managed by Certbot
}

Before I tried adding the proxy_pass (switching the proxy_pass lines to try_files $uri $uri/ =404;) I was getting the default nginx page. I would expect that adding the proxy_pass lines would forward request to the server's port 4200 where the docker container is running. Instead I am getting a 502 Bad Gateway. I am assuming the problem lies in my Ngnix configuration but I am not sure what I am doing wrong? Any help would be great.


Solution

It was a dumb error on my part, but maybe this will help someone in the future. It was in my docker run command.

docker run -d -p 4200:80 my-app:latest

Inside my docker container was listening on 80 but i was sending it to 4200.



Answered By - seanmt13
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing