Nginx反向代理(HTTP-HTTPS)+负载均衡

配置环境:

[root@216700 nginx]# nginx -v

nginx version: nginx/1.16.1

[root@216700 nginx]# uname -a

Linux 216700.70294.sys.ipv4.io3.10.0-1062.1.1.el7.x86_64 #1 SMP Fri Sep 13 22:55:44 UTC 2019 x86_64 x86_64x86_64 GNU/Linux

[root@216700 nginx]# cat/etc/redhat-release

CentOS Linux release 7.7.1908 (Core)

HTTPS代理:

HTTPS:通过NGINX负载均衡器来访问后端两台配置一样的网站(多个站点)

要点:

通过NGINX反向代理后端的真实服务器,需要在NGINX上配置SSL证书,也需要在两台真实服务器上配置一样的SSL证书,并注意以下红色的配置:

拓扑图:

client ---->192.168.2.10(nginx负载均衡)---->node1:192.168.2.11

                           ---->node2:192.168.2.12

NGINX负载均衡器配置文件:

[root@211501 nginx]# cat www.linuxmysql.com.conf

upstream www.linuxmysql.com {

        server 192.168.2.11:443 max_fails=3 fail_timeout=30s;

        server 192.168.2.12:443 max_fails=3 fail_timeout=30s;

}

server

{

    #listen 80;

        listen 443 ssl http2;

    server_name www.linuxmysql.com;

    index index.php index.html index.htmdefault.php default.htm default.html;

    #root /www/wwwroot/www.linuxmysql.com;

 

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则

    #error_page 404/404.html;

    ssl_certificate   /etc/letsencrypt/live/www.linuxmysql.com/fullchain.pem;

    ssl_certificate_key   /etc/letsencrypt/live/www.linuxmysql.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

    ssl_prefer_server_ciphers on;

    ssl_session_cache shared:SSL:10m;

    ssl_session_timeout 10m;

    error_page 497 https://$host$request_uri;

 

    #SSL-END

 

        location / {

        proxy_passhttps://www.linuxmysql.com;

        proxy_next_upstream errortimeout invalid_header http_500 http_502 http_503;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP$remote_addr;

        proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;

       proxy_set_header X-Forwarded-Proto https;

        proxy_redirect off;

        }

 

 

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改

    error_page 404 /404.html;

    error_page 502 /502.html;

    #ERROR-PAGE-END

 

    #PHP-INFO-START  PHP引用配置,可以注释或修改

    include enable-php-72.conf;

    #PHP-INFO-END

 

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效

    include/www/server/panel/vhost/rewrite/www.linuxmysql.com.conf;

    #REWRITE-END

 

    #禁止访问的文件或目录

    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)

    {

        return 404;

    }

 

    #一键申请SSL证书验证目录相关设置

    location ~ \.well-known{

        allow all;

    }

 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        expires      30d;

        error_log off;

        access_log off;

    }

 

    location ~ .*\.(js|css)?$

    {

        expires     12h;

        error_log off;

        access_log off;

    }

    access_log  /www/wwwlogs/www.linuxmysql.com.log;

    error_log  /www/wwwlogs/www.linuxmysql.com.error.log;

}

 

 

节点node1配置:192.168.2.11

[root@216699 nginx]# cat www.linuxmysql.com.conf

server

{

    listen 80;

        listen 443 ssl http2;

    server_name www.linuxmysql.com;

    index index.php index.html index.htmdefault.php default.htm default.html;

    root /www/wwwroot/www.linuxmysql.com;

 

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则

    #error_page 404/404.html;

    ssl_certificate   /www/server/panel/vhost/cert/www.linuxmysql.com/fullchain.pem;

    ssl_certificate_key   /www/server/panel/vhost/cert/www.linuxmysql.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2TLSv1.3;

    ssl_ciphersECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

    ssl_prefer_server_ciphers on;

    ssl_session_cache shared:SSL:10m;

    ssl_session_timeout 10m;

    error_page 497 https://$host$request_uri;

 

 

 

    #SSL-END

 

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改

    #error_page 404 /404.html;

    #error_page 502 /502.html;

    #ERROR-PAGE-END

 

    #PHP-INFO-START  PHP引用配置,可以注释或修改

    include enable-php-72.conf;

    #PHP-INFO-END

 

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效

    include/www/server/panel/vhost/rewrite/www.linuxmysql.com.conf;

    #REWRITE-END

 

    #禁止访问的文件或目录

    location ~^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)

    {

        return 404;

    }

 

    #一键申请SSL证书验证目录相关设置

    location ~ \.well-known{

        allow all;

    }

 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        expires     30d;

        error_log off;

        access_log /dev/null;

    }

 

    location ~ .*\.(js|css)?$

    {

        expires     12h;

        error_log off;

        access_log /dev/null;

    }

    access_log  /www/wwwlogs/www.linuxmysql.com.log;

    error_log  /www/wwwlogs/www.linuxmysql.com.error.log;

}

 

 

节点node2配置:192.168.2.12

[root@216700 nginx]# cat www.linuxmysql.com.conf

server

{

    listen 80;

        listen 443 ssl http2;

    server_name www.linuxmysql.com;

    index index.php index.html index.htmdefault.php default.htm default.html;

    root /www/wwwroot/www.linuxmysql.com;

 

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则

    #error_page 404/404.html;

    ssl_certificate   /www/server/panel/vhost/cert/www.linuxmysql.com/fullchain.pem;

    ssl_certificate_key   /www/server/panel/vhost/cert/www.linuxmysql.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2TLSv1.3;

    ssl_ciphersECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

    ssl_prefer_server_ciphers on;

    ssl_session_cache shared:SSL:10m;

    ssl_session_timeout 10m;

    error_page 497 https://$host$request_uri;

 

    #SSL-END

 

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改

    #error_page 404 /404.html;

    #error_page 502 /502.html;

    #ERROR-PAGE-END

 

    #PHP-INFO-START  PHP引用配置,可以注释或修改

    include enable-php-72.conf;

    #PHP-INFO-END

 

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效

    include/www/server/panel/vhost/rewrite/www.linuxmysql.com.conf;

    #REWRITE-END

 

    #禁止访问的文件或目录

    location ~^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)

    {

        return 404;

    }

 

    #一键申请SSL证书验证目录相关设置

    location ~ \.well-known{

        allow all;

    }

 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        expires     30d;

        error_log off;

        access_log /dev/null;

    }

 

    location ~ .*\.(js|css)?$

    {

        expires     12h;

        error_log off;

        access_log /dev/null;

    }

    access_log  /www/wwwlogs/www.linuxmysql.com.log;

    error_log  /www/wwwlogs/www.linuxmysql.com.error.log;

}

 

HTTP代理:

HTTP:通过NGINX负载均衡器来访问后端两台配置一样的网站(多个站点)

 

拓扑图:

client ---->192.168.2.10(nginx负载均衡)---->node1:192.168.2.11

                                                               ---->node2:192.168.2.12

NGINX负载均衡器配置文件:

[root@211501 nginx]# cat www.linuxmysql.com.conf

upstream www.linuxmysql.com {

        server 192.168.2.11:80 max_fails=3 fail_timeout=30s;

        server 192.168.2.12:80 max_fails=3 fail_timeout=30s;

}

server

{

    #listen 80;

    server_name www.linuxmysql.com;

    index index.php index.html index.htmdefault.php default.htm default.html;

    #root /www/wwwroot/www.linuxmysql.com;

 

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则

    #error_page 404/404.html;

 

    #SSL-END

 

        location / {

        proxy_pass http://www.linuxmysql.com;

        proxy_next_upstream errortimeout invalid_header http_500 http_502 http_503;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP$remote_addr;

        proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_headerX-Forwarded-Proto http;

        proxy_redirect off;

        }

 

 

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改

    error_page 404 /404.html;

    error_page 502 /502.html;

    #ERROR-PAGE-END

 

    #PHP-INFO-START  PHP引用配置,可以注释或修改

    include enable-php-72.conf;

    #PHP-INFO-END

 

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效

    include/www/server/panel/vhost/rewrite/www.linuxmysql.com.conf;

    #REWRITE-END

 

    #禁止访问的文件或目录

    location ~^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)

    {

        return 404;

    }

 

    #一键申请SSL证书验证目录相关设置

    location ~ \.well-known{

        allow all;

    }

 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        expires     30d;

        error_log off;

        access_log off;

    }

 

    location ~ .*\.(js|css)?$

    {

        expires     12h;

        error_log off;

        access_log off;

    }

    access_log  /www/wwwlogs/www.linuxmysql.com.log;

    error_log  /www/wwwlogs/www.linuxmysql.com.error.log;

}

 

 

 

节点node1配置:192.168.2.11

[root@216699 nginx]# cat www.linuxmysql.com.conf

server

{

    listen 80;

    server_name www.linuxmysql.com;

    index index.php index.html index.htmdefault.php default.htm default.html;

    root /www/wwwroot/www.linuxmysql.com;

 

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则

    #error_page 404/404.html;

 

 

 

    #SSL-END

 

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改

    #error_page 404 /404.html;

    #error_page 502 /502.html;

    #ERROR-PAGE-END

 

    #PHP-INFO-START  PHP引用配置,可以注释或修改

    include enable-php-72.conf;

    #PHP-INFO-END

 

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效

    include/www/server/panel/vhost/rewrite/www.linuxmysql.com.conf;

    #REWRITE-END

 

    #禁止访问的文件或目录

    location ~^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)

    {

        return 404;

    }

 

    #一键申请SSL证书验证目录相关设置

    location ~ \.well-known{

        allow all;

    }

 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        expires     30d;

        error_log off;

        access_log /dev/null;

    }

 

    location ~ .*\.(js|css)?$

    {

        expires     12h;

        error_log off;

        access_log /dev/null;

    }

    access_log  /www/wwwlogs/www.linuxmysql.com.log;

    error_log  /www/wwwlogs/www.linuxmysql.com.error.log;

}

 

 

节点node2配置:192.168.2.12

[root@216700 nginx]# cat www.linuxmysql.com.conf

server

{

    listen 80;

    server_name www.linuxmysql.com;

    index index.php index.html index.htmdefault.php default.htm default.html;

    root /www/wwwroot/www.linuxmysql.com;

 

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则

    #error_page 404/404.html;

 

    #SSL-END

 

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改

    #error_page 404 /404.html;

    #error_page 502 /502.html;

    #ERROR-PAGE-END

 

    #PHP-INFO-START  PHP引用配置,可以注释或修改

    include enable-php-72.conf;

    #PHP-INFO-END

 

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效

    include/www/server/panel/vhost/rewrite/www.linuxmysql.com.conf;

    #REWRITE-END

 

    #禁止访问的文件或目录

    location ~^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)

    {

        return 404;

    }

 

    #一键申请SSL证书验证目录相关设置

    location ~ \.well-known{

        allow all;

    }

 

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        expires     30d;

        error_log off;

        access_log /dev/null;

    }

 

    location ~ .*\.(js|css)?$

    {

        expires     12h;

        error_log off;

        access_log /dev/null;

    }

    access_log  /www/wwwlogs/www.linuxmysql.com.log;

    error_log  /www/wwwlogs/www.linuxmysql.com.error.log;

}


分割线
打赏
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS