Nginx tcp reverse agent configuration and installation

Keywords: Nginx SSL PHP OpenSSL

Nginx upgrade supports stream module

Nginx is used as the reverse proxy of tcp. Currently, in versions 1.7 to 1.9, the [nginx ﹐ tcp ﹐ proxy ﹐ module] module needs to be loaded. After 1.9, the [with stream] module can be used.
Because LNMP is used for installation( https://lnmp.org/ ), the method of upgrading nginx directly is adopted.

Directory: lnmp1.5/include/upgrade'nginx.sh
Note that the following modules are compiled when executing the upgrade ABCD nginx script
–with-http_sub_module –with-stream –with-stream_ssl_module

View Nginx version information after programming

[root@ffm ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2o  27 Mar 2018
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-stream --with-stream_ssl_module --with-openssl=/chroot/bak/lnmp1.5/src/openssl-1.0.2o

Separate Nginx download address:
http://nginx.org/download/
http://nginx.org/download/nginx-1.9.4.tar.gz

Nginx configuration

Reverse proxy configuration of tcp and ws:

stream{
     #tcp
     upstream mqttserver{
        #ip_hash;
        server 192.168.1.117:18840 max_fails=1 fail_timeout=10s;
        server 192.168.1.116:18840 max_fails=1 fail_timeout=10s;
     }
     server{                           
         listen 18830;
         proxy_connect_timeout 5s;
         proxy_timeout 30s; 
         ###proxy_timeout 24h; 
         ###This parameter is very useful when using EMQ stress test. The timeout period of the device at the beginning is too long, resulting in {shutdown, connack ﹣ timeout} in a short time
         proxy_pass mqttserver;      
     }


     #ws    
     upstream mqttws { 
        server  192.168.1.117:70830 weight=5 max_fails=1 fail_timeout=10s; 
        server  192.168.1.116:70830 weight=5 max_fails=1 fail_timeout=10s; 
     } 
     server
     {
         listen 8083;
         proxy_connect_timeout 5s;
         proxy_timeout 30s;         
         proxy_pass mqttws;     
     }      
}

But the wss reverse proxy has not been successfully configured in stream, but it has been successfully configured in http module.

http{
     wss Reverse agent configuration
     #wss
     upstream httpmqttwss { 
        server 192.168.1.116:7084 weight=5 max_fails=1 fail_timeout=10s; 
        server 192.168.1.117:7084 weight=5 max_fails=1 fail_timeout=10s; 
     }      

     server
     {
        listen  8084 ssl;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;

        #ssl on;
        ssl_certificate       /usr/local/ssl/web.com.pem;
        ssl_certificate_key   /usr/local/ssl/web.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        #ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
        ssl_prefer_server_ciphers on;    


        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; } 

        include enable-php.conf;
        location /mqtt {
           proxy_pass  https://httpmqttwss;
           proxy_read_timeout 60s;
           proxy_set_header Host $host;
           proxy_set_header X-Real_IP $remote_addr;
           proxy_set_header X-Forwarded-for $remote_addr;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'Upgrade';
        } 
        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /.well-known {
            allow all;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log  /home/wwwlogs/access_ssl.log;
    }    
}

Posted by anthonyv on Fri, 03 Jan 2020 20:48:14 -0800