Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx reverse proxy for MySQL

Tags:

mysql

nginx

I'm trying to use a reverse proxy for mysql. For some reason this doesn't work (where mysql-1.example.com points to a VM with MySQL).

upstream db {
    server mysql-1.example.com:3306;
}

server {
    listen 3306;
    server_name mysql.example.com;

    location / {
        proxy_pass http://db;
    }
}

Is there a correct way to do this? I tried connecting via mysql, but doens't work

like image 389
Allen Avatar asked Aug 27 '15 10:08

Allen


3 Answers

Make sure your config is not held within the http { } section of nginx.conf. This config should be outside of the http {}.

stream {
  upstream db {
    server mysql-1.example.com:3306;
  }

  server {
    listen 3306;
    proxy_pass db;
  }
}
like image 86
samottenhoff Avatar answered Oct 16 '22 15:10

samottenhoff


You're trying to accomplish a TCP proxy with an http proxy, which is wrong.

Nginx can do the TCP load balancing/proxy stuff but the syntax is different.

look at https://www.nginx.com/resources/admin-guide/tcp-load-balancing/ for more info

like image 37
Luca Bruzzone Avatar answered Oct 16 '22 14:10

Luca Bruzzone


It should be possible as of nginx 1.9 using TCP reverse proxies. You need to compile nginx with the --with-stream parameter. Then, you can add stream block in your config like @samottenhoff said in his answer.

For more details, see https://www.nginx.com/resources/admin-guide/tcp-load-balancing/ and http://nginx.org/en/docs/stream/ngx_stream_core_module.html.

like image 38
wwerner Avatar answered Oct 16 '22 14:10

wwerner