一、Nginx的基本信息
Nginx是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。其特点是占有内存少,并发能力强。在连接高并发的情况下,Nginx是Apache服务不错的替代品,能够支持高达 50,000 个并发连接数的响应。
nginx的官方网站:http://nginx.org
二、搭建nginx服务器
1、安装包下载及解压
[root@server2 ~]# tar zxf nginx-1.14.0.tar.gz
2、修改配置文件
(1)不显示版本号
[root@nginx ~]# cd nginx-1.14.0/src/core/
[root@nginx core]# vim nginx.h
(2)关掉debug功能
[root@nginx nginx-1.14.0]# cd auto/cc/
[root@nginx cc]# vim gcc
3、源码安装nginx
(1)安装所需依赖包
[root@nginx nginx-1.14.0]# yum install gcc pcre-devel openssl-devel -y
(2)源码安装三部曲
[root@nginx nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
[root@nginx nginx-1.14.0]# make
[root@nginx nginx-1.14.0]# make install
补充:
configure 支持下面的选项:
--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。
--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。
--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路径。
--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
--group=<group> - 在nginx.conf中没有指定group指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
--builddir=DIR - 指定编译的目录
--with-rtsig_module - 启用 rtsig模块
--with-http_realip_module - 启用 ngx_http_realip_module
--with-http_addition_module - 启用 ngx_http_addition_module
--with-http_sub_module - 启用 ngx_http_sub_module
--with-http_dav_module - 启用 ngx_http_dav_module
--with-http_flv_module - 启用 ngx_http_flv_module
--with-http_stub_status_module - 启用 "server status" 页
--without-http_charset_module - 禁用 ngx_http_charset_module
--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib。
--without-http_ssi_module - 禁用 ngx_http_ssi_module
--without-http_userid_module - 禁用 ngx_http_userid_module
--without-http_access_module - 禁用 ngx_http_access_module
--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module
--without-http_autoindex_module - 禁用 ngx_http_autoindex_module
--without-http_geo_module - 禁用 ngx_http_geo_module
--without-http_map_module - 禁用 ngx_http_map_module
--without-http_referer_module - 禁用 ngx_http_referer_module
--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE。
--without-http_proxy_module - 禁用 ngx_http_proxy_module
--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
--without-http_memcached_module - 禁用 ngx_http_memcached_module
--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module
--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module
--without-http_browser_module - 禁用 ngx_http_browser_module
--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module
--with-http_perl_module - 启用 ngx_http_perl_module
--with-perl_modules_path=PATH - 指定 perl模块的路径
--with-perl=PATH - 指定 perl 执行文件的路径
--http-log-path=PATH - Set path to the http access log
--http-client-body-temp-path=PATH - Set path to the http client request body temporary files
--http-proxy-temp-path=PATH - Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
--without-http - 禁用 HTTP server
--with-mail_ssl_module - 启用 ngx_mail_ssl_module
--with-cc=PATH - 指定 C编译器的路径
--without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE。
--with-pcre=DIR - 指定 PCRE 库的源代码的路径。
--with-openssl=DIR - Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building
--with-debug - 启用调试日志
--add-module=PATH - Add in a third-party module found in directory PATH
4、创建软链接(创建完成后可直接调用)
[root@server nginx-1.14.0]# cd /usr/local/nginx/
[root@server nginx]# ln -s /usr/local/nginx/sbin/nginx /sbin/
编辑text.html页面
[root@server html]# pwd
/usr/local/nginx/html
[root@server html]# vim test.html
在物理机中测试:
三、nginx实现负载均衡
1、配置
(1)修改worker_processes的值
[root@server ~]# cd /usr/local/nginx/conf
[root@server conf]# vim nginx.conf
worker_processes进程数,一般来说 ,拥有几个cpu,就设置几个进程数
查看cpu :lscpu
(2)修改 用户和用户组
首先添加nginx用户并指定家目录
[root@server conf]# useradd -M -d /usr/local/nginx/ -u 800 nginx
当cpu 为两个时 worker_cpu_affinity 01 10;
当cpu为四个时 worker_cpu_affinity 0001 0010 0100 1000;
(3)修改最大链接数
[root@server conf]# vim nginx.conf
在操作系统上进行修改
[root@server conf]# vim /etc/security/limits.conf
(4)设置轮叫uptream
[root@server conf]# vim nginx.conf
添加定位:当访问www.westos.org的80端口时把页面定位到upstream westos
(5)检测语法并重新加载
(6)测试
1)负载均衡算法rr
在测试主机(物理主机)中添加本地解析
开启server3和server4中httpd服务
在物理主机中测试:访问www.westos.org
2)负载均衡算法给定权重:
[root@server ~]# cd /usr/local/nginx/conf/
[root@server conf]# vim nginx.conf
在物理主机中测试:
[root@foundation66 kiosk]# for i in {1..10}; do curl www.westos.org;done
3)负载均衡算法ip_hash(同一个ip访问后端服务器不变)
Nginx中的ip_hash技术能够将某个ip的请求定向到同一个后端web机器中,这样,这个ip下的客户端和某个后端web机器就能建立起稳固的链接
配置nginx服务
[root@server conf]# vim nginx.conf
[root@server conf]# nginx -s reload
[root@server 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@foundation66 kiosk]# for i in {1..10}; do curl www.westos.org;done
(7)健康检查
配置nginx文件
[root@server conf]# vim nginx.conf
[root@server conf]# nginx -s reload
[root@server 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
在浏览器中测试(关闭server3和server4中的httpd服务)
四、在nginx中添加模块sticky
sticky是nginx的一个模块,它时基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使得同一个客户端的请求落在同一台服务器上,默认标识名为route
1.客户端首次发起访问请求,nginx接收后,发现请求头没有cookie,则以轮询方式将请求分发给后端服务器。
2.后端服务器处理完请求,将响应数据返回给nginx。
3.此时nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值
4.客户端接收请求,并保存带route的cookie。
5.当客户端下一次发送请求时,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。
1、关闭nginx
[root@server conf]# nginx -s stop
2、解压nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
[root@server ~]# yum install -y unzip #安装解压工具
[root@server ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip #解压
3、编译安装
[root@server nginx-1.14.0]# make clean #清理缓存
rm -rf Makefile objs
[root@server nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
[root@server nginx-1.14.0]# make && make install
4、修改配置文件
[root@server nginx-1.14.0]# cd /usr/local/nginx/conf/
[root@server conf]# vim nginx.conf
[root@server conf]# nginx
5、测试
在浏览器中测试