Nginx编译安装+配置websocket负载均衡

Nginx编译安装+配置websocket负载均衡

目录

 

  • 一. 安装nginx

  • 二. 启动nginx

  • 三. 配置websocket负载均衡

 

一. 安装nginx

 

1.1 准备一台Ubuntu主机

我选择了通过microsoft store安装的Ubuntu 18.04 LTS

1.2 安装依赖

依次执行下面的5条命令, 安装所有的依赖

sudo apt-get install build-essential

sudo apt-get install libtool  

sudo apt-get install libpcre3 libpcre3-dev

sudo apt-get install zlib1g-dev

sudo apt-get install openssl libssl-dev

1.3 下载nginx

  • 进入opt目录
cd /opt
  • 下载nginx, 将最后的版本号1.18.0, 替换为你需要的版本号即可
sudo wget http://nginx.org/download/nginx-1.18.0.tar.gz

注: 通过wget下载可能需要FQ, 如果下载失败, 可以直接登录http://nginx.org/下载对应的版本源码, 通过ftp等方法拷贝至/opt目录, 并修改其权限

  • 解压缩, 记得替换为你的版本号
sudo tar -xzvf nginx-1.18.0.tar.gz
  • 改名字, 记得替换为你的版本号
sudo mv nginx-1.18.0 nginx

1.4 安装nginx

  • 进入nginx目录
cd /opt/nginx
  • 配置nginx configure

注意:

执行本命令前需要仔细检查粘贴后的结果, 所有空格都为英文的空格, prefix和两个with之前, 都是两个英文的横杠, 即--

执行完毕后, 需要仔细检查命令行输出, 确保没有任何警告或错误

--prefix指定了安装目录, 即/usr/local/nginx

sudo ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  • 安装nginx

注意: makemake install前都需要加上sudo, 否则会因为内部执行mkdir时权限错误, 导致安装失败

sudo make && sudo make install

此时nginx已经被安装到了/usr/local/nginx目录中

 

二.启动nginx

 

  • 进入nginx启动目录
cd /usr/local/nginx/sbin
  • 启动nginx
sudo ./nginx

启动nginx

  • 检查nginx是否已经启动
sudo ps -ef | grep nginx

启动成功:
nginx启动成功

  • 浏览器访问nginx

nginx默认监听80端口

通过浏览器访问localhost:80, 即可看到如下页面:

localhost:80页面

 

三.配置websocket负载均衡

 

3.1 找到nginx的配置文件

sudo /usr/local/nginx/sbin/nginx -t

这条命令的主要作用是检查nginx的配置文件格式是否正确, 在这里, 我们利用控制台打印的信息, 获取了nginx的配置文件路径:

/usr/local/nginx/conf/nginx.conf

注意: 配置文件的路径并不是/opt/nginx/conf/nging.conf, 那个是源码里的默认配置文件, 修改它是没有用的

配置文件路径

  • 备份配置文件

进入配置文件路径

cd /usr/local/nginx/conf

备份配置文件 (修改文件前先备份是好习惯, 使用mv而不是rm也是一个好习惯)

sudo cp nginx.conf nginx.conf_backup
  • 打开配置文件
sudo vim nginx.conf

默认配置文件

3.2 修改配置文件, 实现websocket负载均衡

需要修改的内容如下, 完整的配置文件在本文最后

upstream websocket {
        server localhost:10215;
        server localhost:10222;
    }
location / {
    #root   html;
    #index  index.html index.htm;

    # 名字和upstream的名字保持一致即可
    proxy_pass http://websocket;

    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_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
  • 配置文件解读:

nginx继续监听80端口(你也可以修改为别的端口)

对80端口的访问, 都会应用名为websocket的代理配置

websocket中, 配置了两个地址, 即localhost:10215localhost:10222

根据websocket的默认配置, 将采用轮循的方式实现负载均衡

每个请求按时间顺序轮流转发至不同的服务器, 如果应用服务器宕机, 自动剔除, 剩下的继续轮询.

你也可以通过配置websocket, 以权重ip-哈希算法的方式来实现负载均衡.

新配置文件

3.3 重新加载nginx配置文件

  • 检查配置文件, 确保格式正确
sudo /usr/local/nginx/sbin/nginx -t
  • 重新加载配置文件

注: 执行命令后, 没有消息就是好消息

sudo ./nginx -s reload

此时, websocket负载均衡配置完毕并且已经启动了服务.

所有对localhost:80的访问, 都会被转发至localhost:10215localhost:10222

附1 关闭nginx的方法

sudo ./nginx -s stop

另一种方法是直接杀死nginx进程, 进程pid通过sudo ps -ef | grep nginx查询

sudo kill nginx的pid

附2 nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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;

    # 名字和proxy_pass保持一致即可
    # websocket server地址
    upstream websocket {
        server localhost:10215;
        server localhost:10222;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;

            # 名字和upstream的名字保持一致即可
            proxy_pass http://websocket;

            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_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
posted @ 2020-05-07 08:05  Silenzio  阅读(2713)  评论(0编辑  收藏  举报