V
V
VadimFox2019-12-04 17:04:12
Nginx
VadimFox, 2019-12-04 17:04:12

How to properly configure nginx reverse proxy - docker nginx?

There is a server, it costs nginx to distribute requests, there are also two containers, one of them must accept requests from ex1.example.com, the second from ex2.example.com (to the main site) and ap.ex2.example.com (site admin panel).

Available config (for one of the containers):

external nginx

server {
        listen 80;
        server_name ex1.example.com;
        return 301 https://$host$request_uri;
}
server {
        listen 443;
        server_name ex1.example.com;
        ssl_certificate           /etc/letsencrypt/live/ex1.example.com/fullchain.pem;
        ssl_certificate_key       /etc/letsencrypt/live/ex1.example.com/privkey.pem;
        ssl on;
        ssl_session_cache  builtin:1000  shared:SSL:10m;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;
        access_log            /var/log/nginx/ex1.access.log;
        location / {
                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;

                proxy_pass          http://ex1.example.loc:8081;
                proxy_read_timeout  90;
        }
}


docker nginx

server {
    charset utf-8;
    index index.php;
    listen 80;
    server_name ex1.example.loc;
    error_log  /var/log/nginx/ex1.example.loc/error.log;
    access_log /var/log/nginx/ex1.example.loc/access.log;
    root /var/www/ex1.example.loc;

    location / {
        if (!-e $request_filename){
           rewrite ^(.*)$ /index.php;
        }
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}


and added to the server host
127.0.0.1 ex1.example.loc
Why? I don't know, I think it's easier

As a result, I get 502 =(
Please tell me what is the problem or which way to look

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VadimFox, 2019-12-06
@VadimFox

Everything turned out to be much simpler, the nginx config was named ex2.example.com, of course, it didn’t pick it up automatically, renamed it to default.conf and everything flew ...

Z
zzamzam, 2019-12-04
@Inlore

Is the container's nginx listening on 8081 on 127.0.0.1 of the host (published or using the host's network)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question