0

I have a Centos 7 server. In this server I have Nginx.

In this server I have a Docker which contains my app.

App.yml:

version: '2'
services:
    myBrand-app:
        image: myBrand
        environment:
            - _JAVA_OPTIONS=-Xmx512m -Xms256m
            - SPRING_PROFILES_ACTIVE=prod,swagger
            - SPRING_DATASOURCE_URL=jdbc:postgresql://myBrand-postgresql:5432/myBrand
            - SLEEP=10 # gives time for the database to boot before the application
        ports:
            - 8080:8080
    myBrand-postgresql:
        extends:
            file: postgresql.yml
            service: myBrand-postgresql

Is it possible to access a Docker through the Nginx Proxy through the ip of my server?

I want to use Nginx as a reverse proxy to the container.

  • you do nothing special in your nginx config to point an incoming route to the port of your container ... you should be showing your nginx config and not the docker compose yaml since no change is required in your above docker yaml file – Scott Stensland Mar 11 at 21:08
  • @ScottStensland have you an example please ? – Mercer Mar 12 at 8:48
1

Here is how to configure nginx to route traffic from the internet straight into a local host port where your own server runs.

When you install nginx it will install its default config file often at location

cat /etc/nginx/nginx.conf

near bottom of above file you should see something similar to

    include /etc/nginx/sites-enabled/default;
}

if not just create above default file which can look like below

cat /etc/nginx/sites-enabled/default; 

which can contain

server { 

    listen  80 ;

    server_name   example.com, www.example.com;

    rewrite ^/(.*) https://example.com/$1 permanent; # mysettings
}

# ..................  enduser .................. #

server {  #  redirect www to normal domain

    listen       443  ssl ;

    server_name www.example.com;

    include /etc/nginx/mysettings/include/ssl;

    return 301 https://example.com$request_uri;
}

server {

    listen  443 ssl ;

    include /etc/nginx/mysettings/include/ssl;

    server_name  example.com;

    include /etc/nginx/snippets/nginx_common_location_443;

    location / {

        # route to enduser 

        proxy_pass http://127.0.0.1:3000/;
    }


    include /etc/nginx/mysettings/include/custom_server_include;

}

in the above you see this portion :

    location / {

        # route to enduser 

        proxy_pass http://127.0.0.1:3000/;
    }

which defines a route to direct traffic from outside internet into a specified host and port of my server which in above example lives at 127.0.0.1:3000 ... in your case replace my 3000 with your port 8080 ... so now when a browser goes to

https://example.com

that traffic gets routed to my host port which run at

http://127.0.0.1:3000/

for completeness I now show you some of the helper setting files mentioned in above config file

cat /etc/nginx/myconfig/include/ssl;

which looks like

#
# Based on https://mozilla.github.io/server-side-tls/ssl-config-generator/?server=nginx-1.4.6&openssl=1.0.1f&hsts=yes&profile=modern
ssl_certificate     /mydir/nginx/sslcerts/example.com/fullchain.pem;
ssl_certificate_key /mydir/nginx/sslcerts/example.com/privkey.pem;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:5m;

# Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
ssl_dhparam /etc/ssl/certs/dhparam.pem;

ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';

and another config file here

cat /etc/nginx/snippets/nginx_common_location_443;

which contains

# the following is required for WebSockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

if you have several routes to define you can put them into a file

cat /etc/nginx/myconfig/include/custom_server_include; 

which can look similar to

if ( $request_method !~ ^(GET|POST|PUT|PATCH|DELETE|OPTIONS)$ ) {
    # now send to error
    return 404;
}

location ~* \.(php)$ {
    # matches any request ending in php
    return 403;
}

location /apataki {
    proxy_pass http://localhost:28778/;
}

location /hooks/ {

    # this is my webhook server
    proxy_pass http://localhost:9000/hooks/;
}

# .......

error_page 404 /error_404.html;
location = /error_404.html {
  root  /cryptdata/var/deploy;
}

error_page 502 /error_502.html;
location = /error_502.html {
  root  /cryptdata/var/deploy;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.