站長資訊網
        最全最豐富的資訊網站

        CentOS 7.6配置Nginx反向代理

        一,實驗介紹

        利用三臺CentOS 7虛擬機搭建簡單的Nginx反向代理負載集群,三臺虛擬機地址及功能介紹

        192.168.2.76    nginx負載均衡器

        192.168.2.82    web01服務器

        192.168.2.78    web02服務器

        二,安裝nginx軟件(以下操作三臺虛擬機都要進行)

        有些Centos 7.6里面沒有安裝wget命令,所以要自己安裝:

        yum -y install wget

        安裝nginx軟件:(三個服務器都要安裝)

        $ wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

        $ rpm -ivh epel-release-latest-7.noarch.rpm

        $ yum install nginx (直接yum安裝)

        安裝就這么簡單方便,安裝完成后,就可以使用systemctl來控制nginx的啟動了

        $ systemctl enable nginx (加入開機啟動)
        $ systemctl start nginx (開啟nginx)
        $ systemctl status nginx (查看狀態)

        三臺服務器分別安裝好nginx后測試能否正常運行,提供web服務。如果報錯可能是防火墻的原因,請看最后幾步關于防火墻的。

        修改代理服務器的nginx的配置文件,實現負載均衡。顧名思義就是將多個請求分發到不同的服務上,實現均衡的負載,減小單個服務的壓力。

        $ vi /etc/nginx/nginx.conf  (修改配置文件,全局配置文件)

         

        # For more information on configuration, see:
        #  * Official English Documentation: http://nginx.org/en/docs/
        #  * Official Russian Documentation: http://nginx.org/ru/docs/

        user nginx;
        worker_processes auto; (默認為自動,可以自己設置,一般不大于cpu核數)
        error_log /var/log/nginx/error.log; (錯誤日志路徑)
        pid /run/nginx.pid; (pid文件路徑)

        # Load dynamic modules. See /usr/share/nginx/README.dynamic.
        include /usr/share/nginx/modules/*.conf;

        events {
            accept_mutex on; (設置網路連接序列化,防止驚群現象發生,默認為on)
            multi_accept on; (設置一個進程是否同時接受多個網絡連接,默認為off)
            worker_connections 1024; (一個進程的最大連接數)
        }

        http {
            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; (這里注釋掉)
            tcp_nodelay        on;
            keepalive_timeout  65; (連接超時時間)
            types_hash_max_size 2048;
            gzip on; (開啟壓縮)
            include            /etc/nginx/mime.types;
            default_type        application/octet-stream;

            # Load modular configuration files from the /etc/nginx/conf.d directory.
            # See http://nginx.org/en/docs/ngx_core_module.html#include
            # for more information.
            include /etc/nginx/conf.d/*.conf;

        # 這里設置負載均衡,負載均衡有多中策略,nginx自帶的有輪詢,權重,ip-hash,響應時間等粗略。
        # 默認為平分http負載,為輪詢的方式。
        # 權重則是按照權重來分發請求,權重高的負載大
        # ip-hash,根據ip來分配,保持同一個ip分在同一臺服務器上。
        # 響應時間,根據服務器都nginx 的響應時間,優先分發給響應速度快的服務器。
        集中策略可以適當組合
            upstream tomcat { (tomcat為自定義的負載均衡規則名)
                ip_hash; (ip_hash則為ip-hash方法)

              server 192.168.2.78:80 weight=3 fail_timeout=20s;
              server 192.168.2.82:80 weight=4 fail_timeout=20s;

         

        ## 可以定義多組規則
        }

         

            server {
                listen      80 default_server; (默認監聽80端口)
                listen      localhost; (監聽的服務器)
                server_name  _;
                root        /usr/share/nginx/html;

                # Load configuration files for the default server block.
                include /etc/nginx/default.d/*.conf;

                location / { ( /  表示所有請求,可以自定義來針對不同的域名設定不同負載規則 和服務)
          proxy_pass    http://tomcat; (反向代理,填上你自己的負載均衡規則名)
          proxy_redirect off; (下面一些設置可以直接復制過去,不要的話,有可能導致一些 沒法認證等的問題)
          proxy_set_header Host $host;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_connect_timeout 90; (下面這幾個都只是一些超時設置,可不要)
                  proxy_send_timeout 90;
                  proxy_read_timeout 90;
                }
          # location ~.(gif|jpg|png)$ { (比如,以正則表達式寫) 
          #  root /home/root/images;
          #  }

                error_page 404 /404.html;
                    location = /40x.html {
                }

                error_page 500 502 503 504 /50x.html;
                    location = /50x.html {
                }
            }

        # Settings for a TLS enabled server.
        #
        #    server {
        #        listen      443 ssl http2 default_server;
        #        listen      [::]:443 ssl http2 default_server;
        #        server_name  _;
        #        root        /usr/share/nginx/html;
        #
        #        ssl_certificate “/etc/pki/nginx/server.crt”;
        #        ssl_certificate_key “/etc/pki/nginx/private/server.key”;
        #        ssl_session_cache shared:SSL:1m;
        #        ssl_session_timeout  10m;
        #        ssl_ciphers HIGH:!aNULL:!MD5;
        #        ssl_prefer_server_ciphers on;
        #
        #        # Load configuration files for the default server block.
        #        include /etc/nginx/default.d/*.conf;
        #
        #        location / {
        #        }
        #
        #        error_page 404 /404.html;
        #            location = /40x.html {
        #        }
        #
        #        error_page 500 502 503 504 /50x.html;
        #            location = /50x.html {
        #        }
        #    }
        }

        更新配置后,可以重載配置生效,不需要重啟服務

        nginx -s reload

        如果不能訪問,可能是由于防火墻打開了,端口沒有開啟:

        啟動: systemctl start firewalld
        關閉: systemctl stop firewalld
        查看狀態: systemctl status firewalld
        開機禁用  : systemctl disable firewalld
        開機啟用  : systemctl enable firewalld

        開啟一個端口:

        添加
        firewall-cmd –zone=public –add-port=80/tcp –permanent    (–permanent永久生效,沒有此參數重啟后失效)
        重新載入
        firewall-cmd –reload
        查看
        firewall-cmd –zone= public –query-port=80/tcp
        刪除
        firewall-cmd –zone= public –remove-port=80/tcp –permanent

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 国产精品日本一区二区不卡视频 | 日韩精品成人亚洲专区| 精品欧洲av无码一区二区三区| 国产午夜福利精品久久| 国产精品久久久久久久久免费| 亚洲AV第一页国产精品| 精品91自产拍在线观看二区| 国产精品欧美日韩| 国产精品无码av在线播放| 欧美日韩专区麻豆精品在线 | 国产精品免费网站| 久热这里只有精品12| 久久久久国产成人精品亚洲午夜| 欧美一区二区精品久久| 99精品视频在线观看| 全球AV集中精品导航福利| 亚洲欧美一级久久精品 | 日韩精品久久无码人妻中文字幕| 欧美XXXX黑人又粗又长精品| 国产成人精品日本亚洲专区| 久久精品免费观看| 国产精品欧美久久久天天影视| 国产精品乱码高清在线观看| 欧洲成人午夜精品无码区久久| 亚洲精品无码午夜福利中文字幕| 无码欧精品亚洲日韩一区夜夜嗨| 久久国产精品无码网站| 久久996热精品xxxx| 国产在线观看高清精品| 国产亚洲精品国看不卡| 国产精品嫩草影院一二三区| 亚洲综合一区二区精品导航| 2022国产精品最新在线| 国产精品v欧美精品v日韩| 国产精品高清2021在线| 国产精品99久久精品爆乳| 国产美女精品视频| 久久精品国产亚洲AV不卡| 天天爽夜夜爽夜夜爽精品视频| 日韩精品一区二区三区视频| 无夜精品久久久久久|