[root@server11 nginx]# cd html/
[root@server11 html]# pwd
/usr/local/openresty/nginx/html
[root@server11 html]# cp ~/memcache-4.0.5.2/example.php . ##将测试页放置到openresty中nginx的默认发布目录下
[root@server11 html]# ls
50x.html example.php index.html
[root@foundation Desktop]# ab -c10 -n5000 http://192.168.0.11/example.php
[root@server11 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server11 conf]# vim nginx.conf
http {
upstream memcache {
server 127.0.0.1:11211;
keepalive 512;
}
location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key; ##现在memcache中找,如果有则直接返回,如果没有再通过fastcgi去后端找
srcache_store PUT /memc $key; ##将在后端找到的值存储到memcache中,下次再请求的时候可以直接返回
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
[root@server11 conf]# /usr/local/openresty/nginx/sbin/nginx -t ##检测是否有语法错误
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[root@server11 conf]# /usr/local/openresty/nginx/sbin/nginx -s reload ##重新加载
[root@foundation Desktop]# ab -c10 -n5000 http://192.168.0.11/example.php
[root@server11 conf]# /usr/local/openresty/nginx/sbin/nginx -s stop
[root@server11 ~]# systemctl start nginx.service ##停止openresty中的nginx,开启原生nginx
[root@server11 ~]# cd /usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf
3 worker_processes 2; ## 工作进程数(最好将其设置为可用的CPU内核数)或者,可以将其设置为auto。 这样nginx会自动根据核心数为生成对应数量的worker进程。
4 worker_cpu_affinity 01 10;
14 use epoll;
15 worker_connections 65535; ##单个工作进程并发连接数
[root@server11 conf]# nginx -s reload
[root@server11 conf]# cd /etc/pam.d/
[root@server11 pam.d]# cd ..
[root@server11 etc]# vim /etc/security/limits.conf
62 nginx - nofile 65535
[root@server12 ~]# systemctl start httpd
[root@server12 ~]# cd /var/www/html/
[root@server12 html]# ls
[root@server12 html]# echo server12 > index.html
[root@server12 html]# curl localhost
server12
[root@server13 ~]# systemctl start httpd
[root@server13 ~]# cd /var/www/html/
[root@server13 html]# echo server13 > index.html
[root@server13 html]# curl localhost
server13
01表示启用第一个CPU内核,10表示启用第二个CPU内核;
使用epoll模型
修改用户进程可打开文件数限制
[root@server11 conf]# pwd
/usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf ## 编辑nginx配置文件
20 upstream westos {
21 server 192.168.0.12:80;
22 server 192.168.0.13:80;
23 }
49 # location / {
50 # root html;
51 # index index.php index.html index.htm;
52 # }
54 location / {
55 proxy_pass http://westos;
56 }
[root@server11 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@server11 conf]# nginx -s reload
[root@server11 ~]# curl 192.168.0.11
server13
[root@server11 ~]# curl 192.168.0.11
server12
[root@server11 ~]# tar zxf nginx-1.19.1.tar.gz
[root@server11 ~]# cd nginx-1.19.1/
[root@server11 nginx-1.19.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@server11 nginx-1.19.1]# vim auto/cc/gcc ## 注释debug选项
171 # debug
172 #CFLAGS="$CFLAGS -g"
[root@server11 nginx-1.19.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[root@server11 nginx-1.19.1]# make ##注意不要make install
[root@server11 objs]# cd /usr/local/nginx/sbin/
[root@server11 sbin]# mv nginx nginx.old
[root@server11 ~]# cd nginx-1.19.1/objs/
[root@server11 objs]# cp nginx /usr/local/nginx/sbin/ ##复制新版nginx到相关目录下
[root@server11 objs]# cd /usr/local/nginx/sbin/
[root@server11 sbin]# ls
nginx nginx.old
[root@server11 sbin]# ./nginx.old -v
nginx version: nginx/1.18.0
[root@server11 sbin]# ./nginx -v
nginx version: nginx/1.19.1
[root@server11 sbin]# curl -I localhost
Server: nginx/1.18.0
[root@server11 sbin]# ps ax | grep nginx
[root@server11 sbin]# kill -USR2 18678 ##升级新程序
[root@server11 sbin]# kill -WINCH 18678 ##关闭原worker进程但保留主进程:为了回退
[root@server11 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.19.1
[root@server11 sbin]# ls
nginx nginx.old
[root@server11 sbin]# mv nginx nginx.new ##还原nginx程序
[root@server11 sbin]# mv nginx.old nginx
[root@server11 sbin]# ls
nginx nginx.new
[root@server11 sbin]# kill -HUP 18678 ##唤醒原进程
[root@server11 sbin]# kill -WINCH 26925 ##回收新版本的worker进程
[root@server11 sbin]# curl -I localhost
Server: nginx/1.18.0
构建nginx虚拟主机,可以使在访问同一台主机不同域名的时候访问不同的页面
[root@server11 conf]# pwd
/usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf ##编辑配置文件添加相应设置
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos;
}
}
[root@server11 conf]# nginx -s reload
[root@server11 conf]# kill 18678 26925 27294 27295 27776 27777
[root@server11 conf]# nginx
[root@server11 conf]# curl localhost
[root@foundation file_recv]# vim /etc/hosts
192.168.0.11 server11 www.westos.org www.linux.org ##主机添加解析
[root@server11 conf]# vim nginx.conf
server {
listen 80;
server_name www.linux.org;
location / {
root /web1;
index index.html;
}
}
}
[root@server11 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@server11 conf]# nginx -s reload
[root@server11 conf]# mkdir /web1
[root@server11 conf]# cd /web1/
[root@server11 web1]# echo web1 > index.html ##编辑默认发布页面
[root@foundation file_recv]# ping www.linux.org
192.168.0.11 server11 www.westos.org www.linux.org
[root@foundation file_recv]# curl www.linux.org ##访问www.linux.org 显示web1测试页面
web1
[root@foundation file_recv]# curl www.westos.org ## 访问www.westos.org 负载均衡
server12
[root@foundation file_recv]# curl www.westos.org
server13
[root@server11 web1]# cd -
/usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf ##编辑配置文件
[root@server11 conf]# cd /etc/pki//tls/certs/ ##在相关目录下创建cert.pem文件
[root@server11 certs]# ls
[root@server11 certs]# make cert.pem ##创建证书与key的相关文件
[root@server11 certs]# mv cert.pem /usr/local/nginx/conf/ ##将文件移动到nginx配置文件所在目录中
[root@server11 certs]# nginx -t
[root@server11 certs]# nginx -s reload
[root@server11 certs]# netstat -antlp ##443端口开启
[root@foundation file_recv]# curl 192.168.0.11
[root@foundation file_recv]# curl -k https://192.168.0.11
[root@server11 conf]# vim nginx.conf
22 server 192.168.0.12:80 weight=3; 62
[root@server11 conf]# nginx -s reload
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server13
[root@server12 html]# systemctl stop httpd
[root@server13 html]# systemctl stop httpd
[root@foundation ~]# curl www.westos.org
502 Bad Gateway</title></head>
[root@server11 conf]# vim nginx.conf
24 server 127.0.0.1:80 backup;
[root@server11 conf]# nginx -s reload
[root@foundation ~]# curl www.westos.org
Welcome to nginx!</title>
[root@server12 html]# systemctl start httpd
[root@server13 html]# systemctl start httpd
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server13
当两台后端服务器都挂掉后访问不会报错,会返回nginx测试页。
源地址不变,则后端调度不变,同一个ip过来的请求会发往同一个后端,不会改变。
[root@server11 conf]# vim nginx.conf
21 ip_hash;
[root@server11 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@server11 conf]# nginx -s reload
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server12
注意:添加ip_hash时要注释掉backup选项,因为backup不支持ip_hash算法,同时开启会报错。
[root@server11 ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
[root@server11 ~]# cd nginx-1.18.0/
[root@server11 nginx-1.18.0]# make clean
rm -rf Makefile objs
[root@server11 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 ##添加相关模块重新编译
[root@server11 nginx-1.18.0]# make
[root@server11 nginx-1.18.0]# cd objs/
[root@server11 objs]# nginx -s stop
[root@server11 objs]# cp nginx /usr/local/nginx/sbin/ -f
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y ##复制重新编译好的nginx到相关目录下
[root@server11 objs]# nginx -v
nginx version: nginx/1.18.0
[root@server11 objs]# nginx
[root@server11 objs]# cd /usr/local/nginx/conf
[root@server11 conf]# vim nginx.conf ##编辑配置文件
[root@server11 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@server11 conf]# nginx -s reload
22 sticky;
[root@foundation ~]# curl www.westos.org
server12
[root@foundation ~]# curl www.westos.org
server13
使用curl的方式仍能访问到不同的后端,因为上述基于cookie的算法只在浏览器中生效。
如果不清除浏览器缓存,访问到的始终是一个后端。