Linux下Nginx1.8安装

摘要:nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

 

安装pcre

 

wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz tar-zxvfpcre-8.37.tar.gz cdpcre-8.37 ./configure make makeinstall

 

安装openssl

 

wgethttp://www.openssl.org/source/openssl-1.0.1c.tar.gz tar-zxvfopenssl-1.0.1c.tar.gz

cd openssl-1.0.1c/

./config make makeinstall

 

安装zlib

 

wgethttp://zlib.net/zlib-1.2.8.tar.gz tar-zxvfzlib-1.2.8.tar.gz cdzlib-1.2.8 ./configure make makeinstall

 

下载Nginx1.8至/usr/local,看命令吧:

$tarxzfnginx-1.8.0.tar.gz $./configure--prefix=/home/nginx/nginx $make $makeinstall

1 (96).jpg

若出现cc找不到执行

 

yum-yinstallgccautomakeautoconflibtoolmake

若启动出现如下错误

error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

执行以下命令

ln -s /usr/local/lib/libpcre.so.1 /lib64

安装完成之后生成一个nginx的文件夹,cd进去并切换到sbin目录

启动命令

$./nginx

关闭命令:

$./nginx-sstop

优雅重启

$./nginx-sreload

下面是配置:

1、反向代理配置

修改部署目录下conf子目录的nginx.conf文件(如nginx-1.5.13confnginx.conf)内容,可调整相关配置。

反向代理配置示例:

location/{ #设置主机头和客户端真实地址,以便服务器获取客户端真实IP proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; #禁用缓存 proxy_bufferingoff; #设置反向代理的地址 proxy_passhttp://192.168.1.1; }

代理地址根据实际情况修改。

2、负载均衡配置

nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

负载均衡配置示例:

upstreamtest{ #ip_hash; server192.168.1.251; server192.168.1.252; server192.168.1.247; } server{ listen80; server_namehelloword; location/{ #反向代理的地址 proxy_passhttp://test; } }

Upstream命名和服务器地址根据实际情况修改。

3、负载均衡+反向代理完整配置示例

nginx.conf: worker_processes1; events{ worker_connections1024; } http{ includemime.types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65; upstreamtest{ #ip_hash; server192.168.1.251; server192.168.1.252; server192.168.1.247; } server{ listen80; server_name2; location/{ #设置主机头和客户端真实地址,以便服务器获取客户端真实IP proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; #禁用缓存 proxy_bufferingoff; #反向代理的地址 proxy_passhttp://test; } } }

4、动静分离配置

worker_processes1; events{ worker_connections1024; } http{ includemime.types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65; upstreamtest{ #ip_hash; server192.168.1.251; server192.168.1.252; server192.168.1.247; } server{ listen80; server_name2; #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。 location~.*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)${ root/usr/local/nginx/html/myloan; #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires7d; } #所有jsp、do的动态请求都交给后面的tomcat处理 location~(.jsp)|(.do)${ #tomcat地址 proxy_passhttp://test; proxy_redirectoff; proxy_set_headerHOST$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; client_max_body_size10m; client_body_buffer_size128k; proxy_connect_timeout90; proxy_send_timeout90; proxy_read_timeout90; proxy_buffer_size4k; proxy_buffers432k; proxy_busy_buffers_size64k; proxy_temp_file_write_size64k; } } }

附录:nginx.conf配置详解

#运行用户 userwww-data; #启动进程,通常设置成和cpu的数量相等 worker_processes1; #全局错误日志及PID文件 error_log/var/log/nginx/error.log; pid/var/run/nginx.pid; #工作模式及连接数上限 events{ useepoll;#epoll是多路复用IO(I/OMultiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能 worker_connections1024;#单个后台workerprocess进程的最大并发链接数 #multi_accepton; } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http{ #设定mime类型,类型由mime.type文件定义 include/etc/nginx/mime.types; default_typeapplication/octet-stream; #设定日志格式 access_log/var/log/nginx/access.log; #sendfile指令指定nginx是否调用sendfile函数(zerocopy方式)来输出文件,对于普通应用, #必须设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfileon; #tcp_nopushon; #连接超时时间 #keepalive_timeout0; keepalive_timeout65; tcp_nodelayon; #开启gzip压缩 gzipon; gzip_disable"MSIE[1-6].(?!.*SV1)"; #设定请求缓冲 client_header_buffer_size1k; large_client_header_buffers44k; include/etc/nginx/conf.d/*.conf; include/etc/nginx/sites-enabled/*; #设定负载均衡的服务器列表 upstreammysvr{ #weigth参数表示权值,权值越高被分配到的几率越大 #本机上的Squid开启3128端口 server192.168.8.1:3128weight=5; server192.168.8.2:80weight=1; server192.168.8.3:80weight=6; } server{ #侦听80端口 listen80; #定义使用www.xx.com访问 server_namewww.xx.com; #设定本虚拟主机的访问日志 access_loglogs/www.xx.com.access.logmain; #默认请求 location/{ root/root;#定义服务器的默认网站根目录位置 indexindex.phpindex.htmlindex.htm;#定义首页索引文件的名称 fastcgi_passwww.xx.com; fastcgi_paramSCRIPT_FILENAME$document_root/$fastcgi_script_name; include/etc/nginx/fastcgi_params; } #定义错误提示页面 error_page500502503504/50x.html; location=/50x.html{ root/root; } #静态文件,nginx自己处理 location~^/(images|javascript|js|css|flash|media|static)/{ root/var/www/virtual/htdocs; #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。 expires30d; } #PHP脚本请求全部转发到FastCGI处理.使用FastCGI默认配置. location~.php${ root/root; fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME/home/www/www$fastcgi_script_name; includefastcgi_params; } #设定查看Nginx状态的地址 location/NginxStatus{ stub_statuson; access_logon; auth_basic"NginxStatus"; auth_basic_user_fileconf/htpasswd; } #禁止访问.htxxx文件 location~/.ht{ denyall; } } }