nginx部署指南
(一)依赖检查
nginx依赖了很多第三方的包,在安装之前需要先保证这些依赖的包是否齐全,若不齐全则会导致安装失败,如有互联网的话,可以直接运行下面的语句,使用yum将依赖的包安装上。
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
(二)软件安装
此处采用源代码安装的方式:
(1) 解压
tar -zxvf nginx-1.8.0.tar.gz
(2)编译安装
在解压出来的nginx-1.8.0路径里面,执行下面的预编译。 path指定安装的路径。
./configure --prefix=/path
make install
(3)启动
进入sbin目录下,执行./nginx命令,即可启动。
查看进程: ps -aux |grep 'nginx'
重启nginx: ./nginx -s reopen
停止nginx: ./nginx -s stop
重新载入配置文件: ./nginx -s reload
访问:nginx默认的端口为80。直接访问虚拟机ip,看到nginx欢迎页面表示启动成功。
(三) 配置文件 /conf/nginx.conf
使用负载均衡功能,还需要对/conf/nginx.conf文件进行配置。配置文件如下,可根据需要CUD,实现负载均衡的关键部分用星号注释了。
此处配置的逻辑是,将用户请求分发到192.168.248.1:8082 和192.168.248.1:8081两个端口,且8082权重为3,8081权重为7,权重越高分发到的概率越高。还有一个备用的地址,当81和82都无法访问时,会将请求分发到www.baidu.com。
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
use epoll;
worker_connections 1024;
}
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;
#开启gzip压缩
gzip on;
gzip_disable "MSIE [1-6].";
#************************************ 设定负载均衡的服务器列表 *******************************
upstream mysvr {
#本机上的Squid开启端口
#max_fails,最大失败次数,在访问失败次数超过这个值之后,在fail_timeout时间弄内请求不会分发到该服务器。如fail_timeout位0,则永久失效该服务器
#backup 备份机,所有服务器挂了才会生效
#max_conns 限制分配给某台Server处理的最大连接数量,超过这个数量,将不会分配新的连接给它。默认为0,表示不限制。注意:1.5.9之后的版本才有这个配置
#负载方式:
#方式一:ip_hash ,每个请求会按照访问ip的hash值分配,这样同一客户端连续的Web请求都会被分发到同一server进行处理,可以解决session的问题。但是不能与backup同时使用
#方式二:least_conn ,把请求分配到连接数最少的server
#方式三:weight, weigth参数表示权值,权值越高被分配到的几率越大,可以和方式一结合使用
#方式四: 不写参数,就是默认的轮询方式分配
server 192.168.248.1:8082 weight=3 max_fails=3 fail_timeout=15 max_conns=1000;
server 192.168.248.1:8081 weight=7 max_fails=3 fail_timeout=15;
server www.baidu.com backup;
}
#设定虚拟主机配置
server {
listen 80;
#******************************** 虚拟主机域名 ******************************************
server_name www.lx.com;
#charset koi8-r;
#设定本虚拟主机的访问日志
access_log logs/host.access.log main;
#******************************** 默认请求 ***********************************************
location / {
proxy_pass http://mysvr;
}
#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;
}
#***************************** 静态文件,nginx自己处理 *************************************
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#过期30天,静态文件不怎么更新,过期可以设大一点,
#如果频繁更新,则可以设置得小一点。
expires 30d;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
# 禁止访问 .htxxx 文件
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
(四) 验证
在本地启动两个项目,端口分别是8081和8082 ,访问虚拟服务器上的nginx,可以看到随机的访问到了本地的两个项目,且82的访问概率更高。关闭本地的两个项目,再次刷新nginx的地址访问,发现跳转到了百度的页面,配置ok。