yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
./configure --prefix=/usr/local/nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module(安装ssl模块)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/local/openssl-1.0.2m --with-http_ssl_module --with-pcre=/usr/local/pcre-8.41 --with-zlib=/usr/local/zlib-1.2.11
make
make install
安装nginx的时候遇到如下问题
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using builtin md5 code
+ sha1 library is not found
+ using system zlib library
出现以上问题的原因是,在安装nginx的时候没有指定openssl的解压路径。正确的做法如下:
./configure --prefix=/usr/local/nginx
--with-openssl=/usr/local/openssl-1.1.0g
--with-http_ssl_module
同理,如果pcre和zlib出现类似的问题,指定路径就可:
--with-pcre=/usr/local/pcre-8.41
--with-zlib=/usr/local/zlib-1.2.11
--with-http_stub_status_module
apt-get install build-essential
apt-get install libtool
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g-dev
apt-get install openssl
./configure --prefix=/usr/local/nginx
make
sudo make install
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reopen
./nginx -s reload
./nginx -v
在conf/nginx.conf中配置
#user nobody;#指定nginx worker进程运行用户以及用户组,默认nobody账号运行
worker_processes 1;#nginx要开启的子进程数量,运行过程中监控每个进程消耗内存(一般几M~几十M不等)根据实际情况进行调整,通常数量是CPU内核数量的整数倍
#error_log logs/error.log; #定义错误日志文件的位置及输出级别【debug / info / notice / warn / error / crit】
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;#进程pid存放位置
#nginx工作模式配置
events {
worker_connections 1024;#单个后台worker process进程的最大并发链接数
}
#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;#激活tcp_nopush参数可以允许把httpresponse header和文件的开始放在一个文件里发布。积极的作用是减少网络报文段的数量
#keepalive_timeout 0;#连接超时时间,单位是秒
keepalive_timeout 65;
#gzip on;#开启gzip压缩功能
#server模块
#基于域名的虚拟主机
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#访问路由的配置
location / {
root html;#站点根目录,即网站程序存放目录
index index.html index.htm;
}
#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;
}
# 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
#
#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;
# }
#}
}
http{
……
#配置负载均衡服务器列表
upstream localweb{
server localhost:8081;
server localhost:8082;
server localhost:8083;
}
server {
listen 80;
server_name 192.168.1.101;
location / {
proxy_pass http://localweb;#请求转向localweb定义的服务器列表
#以下是一些反向代理的配置(可选择性配置)
#proxy_redirect off;
proxy_set_header Host $host;#$host是指请求主机头字段,否则为服务器名称。
proxy_set_header X-Real-IP $remote_addr;#$remote_addr是指客户端的IP地址
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
}
}
……
}
可以看出,默认负载均衡算法是轮询。
或者配置为其他算法
upstream localweb{
ip_hash; #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
server localhost:8081;
server localhost:8082;
server localhost:8083;
}
upstream localweb{
#weigth参数表示权值,权值越高被分配到的几率越大
server localhost:8081 weight=5;
server localhost:8082 weight=2;
server localhost:8083 weight=1;
}