1.修改进程数
[root@server1 system]# nginx -s stop
[root@server1 system]# cd /usr/local/nginx/conf/
[root@server1 conf]# ls
[root@server1 conf]# vim nginx.conf
user nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
[root@server1 conf]# ps ax
2.修改open files值
此时的open files数为1024
想将open files值改为65535,需要两步:
1、修改主配置文件
2、修改文件/etc/security/limits.conf
vim /etc/security/limits.conf
nginx - nofile 65535
为安全和方便,我们为nginx添加一个用户
[root@server1 conf]# useradd nginx
[root@server1 conf]# usermod -s /sbin/nologin nginx
[root@server1 conf]# id nginx
uid=1000(nginx) gid=1000(nginx) groups=1000(nginx)
[root@server1 conf]# nginx
[root@server1 conf]# ps axu
切换用户 查询可看到open files 值更改为65535
3.在主配置文件添加负载均衡器和反向代理
vim nginx.conf
[root@server1 conf]# vim nginx.conf
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65535;
}
http {
upstream westos{
server 172.25.52.2:80;
server 172.25.52.3:80;
server 172.25.52.1:8080 backup;
}
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;
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos; ##反向代理
}
#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;
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload
[root@server1 conf]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@server1 conf]# systemctl start httpd
[root@server1 conf]# curl localhost:8080
server1
在客户端访问:
在真机中进行地址解析,curl可以看到负载均衡
[root@foundation52 ~]# vim /etc/hosts
172.25.52.1 server1 www.westos.org
sticky
在一个局域网内有3台电脑,他们有3个内网IP,但是他们发起请求时,却只有一个外网IP,是电信运营商分配在他们连接那个路由器上的,如果使用 ip_hash 方式,则Nginx会将请求分配到不同上游服务器,如果使用 sticky 模块,则会把请求分配到办法cookie的服务器上,实现:内网nat用户的均衡。这是iphash无法做到的。
工作原理:
Sticky是基于cookie的一种负载均衡解决方案,通过分发和识别cookie,使来自同一个客户端的请求落在同一台服务器上,默认cookie标识名为route
首先停止nginx
[root@server1 nginx-1.20.1]# nginx -s stop
[root@server1 nginx-1.20.1]# ps aux| grep nginx
[root@server1 nginx-1.20.1]# cd nginx-1.20.1/objs/
[root@server1 objs]# du -h nginx
3.8M nginx
随后添加sticky,并注释掉backup
[root@server1 ~]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf
user nginx nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65535;
}
http {
upstream westos{
sticky; ###添加sticky
server 172.25.52.2:80;
server 172.25.52.3:80;
#server 172.25.52.1:8080 backup; ##注释(不注释语法会出错)
}
include mime.types;
default_type application/octet-stream;
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload
[root@server1 conf]# curl localhost -I