Nginx配置 、简单服务部署实现负载均衡

一、目标

负载均衡实现高性能web服务器

二、Nginx的介绍:

参考此链接
https://blog.csdn.net/weixin_42812527/article/details/106120808

三、服务器环境

服务器 角色 环境说明
192.168.1.93 负载均衡服务器 系统:Ubuntu 16.04.4
192.168.1.98 开发服务器 系统:Ubuntu 16.04.4
192.168.1.99 开发服务器 系统:Ubuntu 16.04.6

四、Nginx的下载

4.1、第一种方式

sudo apt install nginx

4.2第二种方式

一、下载:
wget http://nginx.org/download/nginx-1.4.2.tar.gz #下载安装包
tar -zxvf nginx-1.4.2.tar.gz #进行解压

二、安装:
./configure --prefix=/usr/local/nginx #配置安装目录
make && make install #进行安装
注:安装目录为/usr/local/nginx

三、安装成功之后会显示下边四个文件夹
conf  #配置文件所放的位置
html   #html文件
logs   #日志文件
sbin #主要二进制程序
四、启动
./sbin/nginx
五、查看是否启动
ps -ef | grep nginx 
成功:
root     64144     1  0 14:21 ?        00:00:00 nginx: master process nginx
xxx+ 64145 64144  0 14:21 ?        00:00:01 nginx: worker process
xxx+ 64146 64144  0 14:21 ?        00:00:01 nginx: worker process
xxx+ 64147 64144  0 14:21 ?        00:00:01 nginx: worker process
xxx+ 64148 64144  0 14:21 ?        00:00:01 nginx: worker process

五、修改配置文件

5.1、配置文件路径

第一种方式下载 配置文件在:/etc/nginx/nginx.conf
第二种方式下载 配置文件在:/usr/local/nginx/nginx.conf
作者 采用第一种方式下载的,所以配置路径在:/etc/nginx/nginx.conf

5.2、修改配置文件

user xugaopeng;
worker_processes 1; //1个工作的子进程,可以自己设置或者 auto
pid /run/nginx.pid;

events {
    
     # 一般是配置nginx连接的特性
        worker_connections 1024; // 这是指 一个子进程最大允许连1024个连接,可以自行修改。
        # multi_accept on;
}

http {
    
      // 服务器配置

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;  // 日志文件
        error_log /var/log/nginx/error.log;  // 错误日志文件

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
		
		// 自己添加如下
        upstream myproject {
    
    
        server 192.168.1.98:9192 weight=3; // 加权轮询。
        server 192.168.1.99:9192;
        }
        server {
    
    
        listen 8008;
        server_name 192.168.1.93;
        location / {
    
    
        proxy_pass http://myproject;
        }
        }


}


#mail {
    
    
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
    
    
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
    
    
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

// 注意、作者在这里自己修改的内容 通过 // 注释

六、开启、关闭 Nginx

一、开启
	cd /etc/nginx
	执行 nginx
二、关闭
	cd /etc/nginx
	执行 nginx -s stop

七、参考文章

https://www.cnblogs.com/xiugeng/p/10155283.html#_label0_3
https://www.nginx.cn/nginxchscommandline#command
https://www.nginx.cn/doc/example/loadbanlance.html

八、总结

作者这里主要是配置了 Nginx 负载均衡,不是很难。基本上按照流程来就行了,如果遇到问题,可以自己百度。

猜你喜欢

转载自blog.csdn.net/weixin_42812527/article/details/106126695