一、安装pcre
1、主页地址:http://www.pcre.org/
下载pcre-7.8.tar.bz2
2、解压缩:
tar xjpf pcre-7.8.tar.bz2
3、配置:
cd pcre-7.8 ./configure --prefix=/usr/local/pcre --libdir=/usr/local/lib/pcre --includedir=/usr/local/include/pcre
configure有许多参数可配,具体参见./configure --help及手册
4、编译:
make
5、安装:
make install
6、检查:
ls /usr/local ls /usr/local/lib ls /usr/local/include
检查是否有pcre目录
二、安装nginx
./configure make make install
默认安装的路径是/usr/local/nginx
-------------------------------------------------
更多的安装配置
./configure --prefix=/usr/local/nginx --with-openssl=/usr/include (启用ssl,源码目录!) --with-pcre=/usr/include/pcre/ (启用正规表达式, 源文件目录!) --with-http_stub_status_module (安装可以查看nginx状态的程序) --with-http_memcached_module (启用memcache缓存) --with-http_rewrite_module (启用支持url重写)
OR
./configure --prefix=/usr/local/nginx \ --with-openssl=/home/sunjl/openssl-0.9.8e \ --with-pcre=/home/sunjl/pcre-8.32 \ --with-http_ssl_module \ --with-http_stub_status_module
--------------------------------------------------
三、启动及重启
启动:/usr/local/nginx/sbin/nginx
停止:kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
重启:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
测试配置文件:/usr/local/nginx/sbin/nginx -t
重载:/usr/local/nginx/sbin/nginx -s reload
测试HTTP头:
curl https://10.47.0.51/pms -v
----------------------------------
四、设置反向代理配置
1、创建配置文件
#vi /usr/local/nginx/conf/proxy.conf server { listen 80; server_name www.example.com; location / { proxy_pass http://192.168.1.200:8080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; } }
2、加载反向代理配置
在http{}块中的第1个server{}块
include /usr/local/nginx/conf/proxy.conf;
加载反向代理配置
这里先在server{}块中
# vim /usr/local/nginx/conf/nginx.conf
3、生成SSL证书
---------------------------SSL
[root@localhost ssl]# openssl genrsa -out privkey.pem 2048
Generating RSA private key, 2048 bit long modulus
............................................................+++
..........+++
e is 65537 (0x10001)
[root@localhost ssl]# ls
privkey.pem
[root@localhost ssl]# openssl req -new -x509 -key privkey.pem -out cacert.pem -days 3650
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:CN
State or Province Name (full name) [Berkshire]:Hunan
Locality Name (eg, city) [Newbury]:Changsha
Organization Name (eg, company) [My Company Ltd]:ZJ
Organizational Unit Name (eg, section) []:Information Department
Common Name (eg, your name or your server's hostname) []:Z
Email Address []:test@test.com
4、开机自启动nginx
这里使用的是编写shell脚本的方式来处理
vi /etc/init.d/nginx (输入下面的代码) #!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /usr/local/nginx/logs/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
:wq 保存并退出
设置文件的访问权限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
#chmod +x /etc/init.d/nginx
#/sbin/chkconfig nginx on
检查一下
#sudo /sbin/chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
5、带SSL的配置文件
server { listen 8100; server_name 100.40.90.181; ssl on; ssl_certificate /usr/local/nginx/conf/ssl/cacert.pem; ssl_certificate_key /usr/local/nginx/conf/ssl/privkey.pem; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } #Force http to https error_page 497 https://$host:$server_port$request_uri; # re-write redirects to http as to https, example: /home proxy_redirect http:// https://; #platform location /platform { proxy_pass http://127.0.0.1:8080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto https; proxy_set_header Host $host:8100; } #WS location /WS { proxy_pass http://100.40.90.40:18082; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Proto https; proxy_set_header Host $host:8100; } #bugfree location /bugfree { proxy_pass http://100.17.20.148:10080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host:8100; proxy_set_header X-Forwarded-Proto https; } }
(三)记录真实的ip地址
代理补充
如果要后端记录来源地址的真实IP而不是前端代理的IP.如果后端使用nginx做web可以在nginx配置中
http {
set_real_ip_from 192.168.1.200;
real_ip_header X-Real-IP;
}
对应代理的配置
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
---------------------------------------------------------
以下未验证
---------------------------------------------------------
五、负载均衡配置
负载均衡就是多个站点做一组反向代理
1、 http {}块中配置
# vim /usr/local/etc/nginx/nginx.conf
http {
…
upstream cluster_server_com {
server 172.17.2.192:80 max_fails=2 fail_timeout=30s;
server 172.17.2.190:80 max_fails=2 fail_timeout=30s;
}
….
}
2、 server{}块使用前面配置的cluster_server_com,虚拟站点中
# vim /usr/local/etc/nginx/vhosts/www.example.com.conf
server {
…
location / {
proxy_pass http://cluster_server_com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
...
}
3、 访问测试
可以看到是以此轮询到后端两台服务器
4、nginx的upstream目前支持4种方式的分配
1)、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2)、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
2)、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
3)、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4)、url_hash(第三方
upstream cluster_server_com {#定义负载均衡设备的Ip及设备状态
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
每个设备的状态设置为:
a)
down 表示单前的server暂时不参与负载
b)
weight 默认为1.weight越大,负载的权重就越大。
c)
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
d)
fail_timeout:max_fails次失败后,暂停的时间。
e)
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
转载于:https://blog.51cto.com/sashimiscreed/1216700