一、nginx的安装
1.首先安装缺少的依赖包:
# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
2.源码包的下载,我安装的是nginx-1.10.3版本,我已经下载好保存在我的网盘中
也可从直接从官网下载稳定版本:
3.nginx的安装(三把火: .config make make install)
[root@cachets nginx-1.10.3]# pwd/usr/local/src/nginx-1.10.3[root@cachets nginx-1.10.3]# ./configure --prefix=/usr/local/nginx-1.10 --with-pcre \> --with-http_stub_status_module --with-http_ssl_module \> --with-http_gzip_static_module --with-http_realip_module \> --add-module=../nginx_upstream_check_module-0.3.0 [root@cachets nginx-1.10.3]# make && make install
常用编译选项说明
nginx大部分常用模块,编译时./configure --help
以--without
开头的都默认安装。
--prefix=PATH
: 指定nginx的安装目录。默认/usr/local/nginx
--conf-path=PATH
: 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf--user=name
: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。--group=name
类似--with-pcre
: 设置PCRE库的源码路径,如果已通过yum方式安装,使用--with-pcre
自动找到库文件。使用--with-pcre=PATH
时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure
和make
来完成。perl正则表达式使用在location
指令和ngx_http_rewrite_module
模块中。--with-zlib=PATH
: 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module
时需要使用zlib 。--with-http_ssl_module
: 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装--with-http_stub_status_module
: 用来监控 Nginx 的当前状态--with-http_realip_module
: 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址--add-module=PATH
: 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)
4. nginx的启动 ./sbin/nginx
### nginx的默认安装路径是/usr/local/nginx[root@myserver nginx]# pwd/usr/local/nginx[root@myserver nginx]# ./sbin/nginx
停止:./sbin/nginx -s stop 也可以kill掉nginx进程 包括master和worker进程
重启: ./sbin/nginx -s reload 一般改完配置文件就可以调用该命令重启
[root@myserver nginx]# ./sbin/nginx -s reload[root@myserver nginx]#
二、用nginx实现负载均衡
1.我在120.77.148.74机器上安装了tomcat8.5,在120.77.153.204上安装了tomcat8.0
2.修改/usr/local/nginx/conf/目录下的nginx.conf配置文件
[root@wenhaijin conf]# vim nginx.conf###主要配置如下:在http{}中加入 upstream demo{ #ip_hash; server 120.77.148.74:8080; server 120.77.153.204:8080; }###在http中的server{}中修改以下内容 location / { proxy_pass http://demo; }
nginx.conf中具体的配置如下
#以用户root来执行nginxuser root;#工作进程数量:配置为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;#事件核心模型 centOs一般采用epoll多路复用机制events { #每个工作进程的最大连接数 worker_connections 1024;}#http核心模块配置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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #可以用weight指定权重 upstream demo{ #ip_hash; server 120.77.148.74:8080; server 120.77.153.204:8080; } server { #nginx默认监听80端口 listen 80; server_name localhost; charset utf-8; #access_log logs/host.access.log main; location / { #注意与upstream对应关系 proxy_pass http://demo; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
3.修改完配置文件后重启nginx服务:
./sbin/nginx -s reload
4.再次访问nginx服务器
刷新页面:
说明已经实现负载均衡
另附nginx快速入手开发文档下载地址: