Home > Back-end >  How to use nginx (intsalled on Docker) to reverse proxy MySQL (installed on Docker too)
How to use nginx (intsalled on Docker) to reverse proxy MySQL (installed on Docker too)

Time:01-30

Here is docker-compose.yaml

services:
  nginx:
    image: nginx:1.23.3
    container_name: nginx
    volumes:
      - /lnmpg/nginx/conf.d:/etc/nginx/conf.d:ro,cached
      - /lnmpg/nginx/nginx.conf:/etc/nginx/nginx.conf :ro
      - /lnmpg/nginx/log:/var/log/nginx
      - /lnmpg/nginx/html:/usr/share/nginx/html
    ports:
      - "80:80"
      - "443:443"
      - "33060:3306"
    environment:
        - TZ=Asia/Shanghai
    restart: always
    networks:
      - nginx-mysql
  mysql:
      image: mysql:5.7.41
      # ports:
      # - "3307:3306"
      environment:
          - TZ=Asia/Shanghai
          - MYSQL_ROOT_PASSWORD=Xqw@1023
      volumes:
          - /lnmpg/mysql/init:/docker-entrypoint-initdb.d
          - /lnmpg/mysql/my.cnf:/etc/mysql/my.cnf :ro
          - /lnmpg/mysql/data:/var/lib/mysql
          - /lnmpg/mysql/log:/var/log/mysql 
      restart: always
      networks:
        - nginx-mysql

And here is nginx.conf

stream {
    log_format basic '$remote_addr [$time_local] '
                     '$protocol $status $bytes_sent $bytes_received '
                     '$session_time';
    access_log logs/stream-access.log debug buffer=32k;
    server {
        listen 33060;
        proxy_pass mysql:3306;
           }  
       }

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

I don't know what went wrong,just could not connect to mysql with port 33060 of the host.If I cancel the ports comment in docker-compose.yml, I can access it normally with 3307.These two containers are under the same network: nginx-mysql,so mysql:3306 in the nginx.conf should be recognized by the docker container as mysql's ip:3306 Did I misunderstand the function of the network or my configuration files were wrong? By the way these two dockers are in one docker-composer file.So networks is not necessary,Right?

CodePudding user response:

What you're doing should work. I can spot a small error though, and that is that you map port 3306 on the Nginx container, but it listens on port 33060. I think I'd make Nginx listen on 3306, by changing the line in the config file to

listen 3306;

If you prefer, you can also change the docker-compose file, so it maps port 33060 in the container instead.

CodePudding user response:

You can't do this. nginx is mainly a proxy for HTTP (hypertext transfer protocol). It won't help you connecting to a totally different protocol like MYSQL's.

Maybe you can explain in your question why you are trying to proxy MySQL access in the first place.

  • Related