nginx链接mysql主要就是需要steam模块,使用yum安装不会安装此模块所以需要编译安装
1 安装nginx的依赖库
[root@myhost41 ~]# yum -y install pcre pcre-devel zlib zlib-devel  openssl openssl-devel
2 创建nginx用户
[root@myhost41 ~]# useradd nginx -s /sbin/nologin -M
[root@myhost41 ~]# id nginx
uid=1008(nginx) gid=1008(nginx) 组=1008(nginx)
3 解压nginx安装包
[root@myhost41 ~]# cd /home/tools/
[root@myhost41 tools]# ll
-rw-r--r--. 1 root root   1032630 6月   8 2020 nginx-1.16.1.tar.gz
[root@myhost41 tools]# tar xf nginx-1.16.1.tar.gz 
[root@myhost41 tools]# ll
drwxr-xr-x. 8 test 1001      4096 8月  13 2019 nginx-1.16.1
-rw-r--r--. 1 root root   1032630 6月   8 2020 nginx-1.16.1.tar.gz
[root@myhost41 tools]# cd nginx-1.16.1
[root@myhost41 nginx-1.16.1]# ll
总用量 756
drwxr-xr-x. 6 test 1001   4096 1月   8 11:02 auto
-rw-r--r--. 1 test 1001 296463 8月  13 2019 CHANGES
-rw-r--r--. 1 test 1001 452171 8月  13 2019 CHANGES.ru
drwxr-xr-x. 2 test 1001   4096 1月   8 11:02 conf
-rwxr-xr-x. 1 test 1001   2502 8月  13 2019 configure
drwxr-xr-x. 4 test 1001     68 1月   8 11:02 contrib
drwxr-xr-x. 2 test 1001     38 1月   8 11:02 html
-rw-r--r--. 1 test 1001   1397 8月  13 2019 LICENSE
drwxr-xr-x. 2 test 1001     20 1月   8 11:02 man
-rw-r--r--. 1 test 1001     49 8月  13 2019 README
drwxr-xr-x. 9 test 1001     84 1月   8 11:02 src
4 编译安装nginx

注意: --with-stream --with-stream_ssl_module


[root@myhost41 nginx-1.16.1]# cd  /usr/local/nginx/conf/
[root@myhost41 conf]# ll
总用量 72
-rw-r--r--. 1 root root 1077 1月   8 11:10 fastcgi.conf
-rw-r--r--. 1 root root 1077 1月   8 11:10 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 1月   8 11:10 fastcgi_params
-rw-r--r--. 1 root root 1007 1月   8 11:10 fastcgi_params.default
-rw-r--r--. 1 root root 2837 1月   8 11:10 koi-utf
-rw-r--r--. 1 root root 2223 1月   8 11:10 koi-win
-rw-r--r--. 1 root root 5231 1月   8 11:10 mime.types
-rw-r--r--. 1 root root 5231 1月   8 11:10 mime.types.default
-rw-r--r--. 1 root root 1156 1月   8 11:16 nginx.conf
-rw-r--r--. 1 root root  515 1月   8 11:13 nginx.conf_bak20210108
-rw-r--r--. 1 root root 2656 1月   8 11:10 nginx.conf.default
-rw-r--r--. 1 root root  636 1月   8 11:10 scgi_params
-rw-r--r--. 1 root root  636 1月   8 11:10 scgi_params.default
-rw-r--r--. 1 root root  664 1月   8 11:10 uwsgi_params
-rw-r--r--. 1 root root  664 1月   8 11:10 uwsgi_params.default
-rw-r--r--. 1 root root 3610 1月   8 11:10 win-utf

[root@myhost41 conf]# vi nginx.conf
[root@myhost41 nginx-1.16.1]# ./configure 
--user=nginx --group=nginx \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \ 
--with-http_realip_module \ 
--with-http_flv_module \ 
--with-http_mp4_module \ 
--with-http_gzip_static_module \ 
--with-stream \ 
--with-stream_ssl_module 

[root@myhost nginx-1.16.1]# make && make install
[root@myhost nginx-1.16.1]# echo $?
0
5 编辑修改nginx配置文件
user  nginx;
worker_processes  1;

error_log  logs/error.log warn;
pid       logs/nginx.pid;

events {
    worker_connections  1024;
}

#-----------------往下--------------------
#四层负载,四层的负载不在http模块里面,他是和http模块同级别的
stream {        
        upstream mysql {
            server 192.168.2.56:3306;  #后端数据库的ip和端口,如果进行了域名解析,直接写域名就好
        }
        server {
            listen 3306;   #如果监听3306,远程登录的时候不用加-p参数
            proxy_connect_timeout 10s;
            proxy_timeout 30s;
            proxy_pass mysql;
        }
}

#------------------往上--------------------

http {
    include       ../conf/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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  0;
    #keepalive_timeout  65;

    #gzip  on;

}
6 启动或重启nginx
[root@myhost41 conf]#  chown -R  nginx:nginx /usr/local/nginx/
[root@myhost41 conf]# /usr/local/nginx/sbin/nginx 
[root@myhost41 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@myhost41 conf]# netstat -lntup |grep nginx
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      29234/nginx: master ]
7 连接测试