一、背景与简介
背景:根据工智道目前的2B系统部署体系,服务器的稳定与安全是一个很大的挑战。为实现灵活的部署方案,以应付不同的震荡(例如某云全部宕机,部署在其他云上的服务器仍然能继续工作),负载均衡技术是企业通常选择较多的方案。
简介:负载均衡又分为四层负载均衡和七层负载均衡。四层负载均衡工作在OSI模型的传输层,主要工作是转发,它在接收到客户端的流量以后通过修改数据包的地址信息将流量转发到应用服务器。 在选择方面,硬件负载均衡性能优越,功能全面,但价格昂贵,一般适合初期或者土豪级公司长期使用。因此软件负载均衡在互联网领域大量使用。常用的软件负载均衡软件有Nginx、LVS、HaProxy等。此贴,使用Nginx搭建服务。
二、下载Nginx部署包
去官网上下载,Nginx有自己的官网。可以百度,也可以直接输入地址http://nginx.org/。
下载时注意你的操作系统,本案例使用的是windows系统,我使用版本为Windows-1.15.9。
三、命令启动服务
下载完以后,包里面有可执行文件,可以双击。也可以使用命令行:
启动:start nginx.exe
停止:nginx -s stop
重新加载: nginx -s reload
四、重点一,破“坑”
启动的时候,经常会遇到一闪而过。首先,nginx是有记录日志的机制的,所以不要慌,可以打开\nginx-1.15.9\logs文件夹下的error.log 查看报错。
(1)我遇到的错误1:
信息:“2019/03/06 13:50:42 [emerg] 792#16152: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)”
问题点:产生这个问题的原因一般有2,要么防火墙 要么80端口被占用
解决:
1、关闭防火墙
2、有关80端口,我用的是windows平台,可以在命令行中输入 C:\>netstat -aon|findstr "80" 命令,来查看目前使用的端口。如下:
最后一个数字很重要,是PID,后面会用到。
一般来说,你找到你写过的程序,关掉使用就行。但是,由于我电脑里装的软件比较多,各种服务都有。所以今天我一开始及时关闭了IIS的80端口,也仍然有问题。
接下来我发现是PID=4的占据了80端口,于是打开 任务管理器=>选择菜单查看=>选择列=>勾选PID ,结果看到对应的是“System”,这个进程,悲剧了!不能直接强制结束掉。
最后,我通过网上查找,发现,原来是sqlserver reporting services服务 占用进程。直接去服务里把这个给停掉,搞定!
五、启动成功
启动成功后,logs下面会多了一个nginx.pid文件,cmd窗口了输入D:\Downloads\nginx-1.15.9>tasklist /fi “imagename eq nginx.exe”
这时会看到两个nginx的进程,为什么是2个(因为nginx是守护进程机制)
六、重点二,配置文件,注释讲解(D:\Downloads\nginx-1.15.9\conf)
nginx.conf文件:
#运行用户
#user nobody;
#开启进程数 <=CPU数
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 {
#Linux下打开提高性能
#use epoll;
#每个进程最大连接数(最大连接=连接数x进程数)
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;
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#打开发送文件
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#客户端上传文件大小控制
client_max_body_size 8m;
#打开gzip压缩
#gzip on;
#设定负载均衡的服务器列表
#upstream mysvr {
# #weigth参数表示权值,权值越高被分配到的几率越大
# #本机上的Squid开启3128端口
# #server 192.168.8.1:3128 weight=5;
# #server 192.168.8.2:80 weight=1;
# #server 192.168.8.3:80 weight=6;
#}
#第一个虚拟主机
server {
#监听IP端口
listen 80;
#主机名
server_name localhost;
#root
#设置字符集
#charset koi8-r;
#本虚拟server的访问日志 相当于局部变量
#access_log logs/host.access.log main;
#日志文件输出格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
location / {
root html;
index index.html index.htm;
}
#静态文件缓存时间设置
#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${
# expires 30d;
#}
#静态文件缓存时间设置
#location ~ .*\.(js|css)?${
# expires 1h;
#}
#对本server"/"启用负载均衡
#location / {
# proxy_pass http://mysvr;
# 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;
# client_max_body_size 10m;
# client_body_buffer_size 128k;
# proxy_connect_timeout 90;
# proxy_send_timeout 90;
# proxy_read_timeout 90;
# proxy_buffer_size 4k;
# proxy_buffers 4 32k;
# proxy_busy_buffers_size 64k;
# proxy_temp_file_write_size 64k;
#}
#设定查看Nginx状态的地址
#location /NginxStatus {
# stub_status on;
# access_log on;
# auth_basic “NginxStatus”;
# auth_basic_user_file conf/htpasswd;
#}
#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 localhost:8666;
#主机名
server_name LIULJ2576;
#WEB文件路径
root E:/Portal;
#默认首页
index HomePage.html;
#location / {
# #这里相当于局部变量
# root E:/Portal;
# index HomePage.html;
#}
}
# HTTPS server HTTPS SSL加密服务器
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}