Loading
0

Nginx反向代理、缓存、负载均衡服务器构建

注:nginx的所有模块必须在编译的时候添加,不能再运行的时候动态加载。
优化nginx程序的执行路径

[root@hexiaoshuai nginx-1.10.2]# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/
[root@hexiaoshuai nginx-1.10.2]# mkdir -p /var/tmp/nginx/client
[root@hexiaoshuai nginx-1.10.2]# chown -R www:www /var/tmp/nginx
[root@hexiaoshuai nginx-1.10.2]# nginx -t
nginx: the configuration file /usr/local/nginx1.10/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.10/conf/nginx.conf test is successful

2、编写nginx服务脚本:

[root@hexiaoshuai ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx1.10/sbin/nginx"
PIDF="/usr/local/nginx1.10/logs/nginx.pid"
case "$1" in
start)
netstat -anplt |grep ":80" &>/dev/null &&pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
echo "Nginx service already running."
else
     $PROG -t&> /dev/null
if [ $? -eq 0 ] ; then 
       $PROG
echo "Nginx service start success."
else
     $PROG -t
fi
fi
   ;;
stop)
netstat -anplt |grep ":80" &>/dev/null &&pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
kill -s QUIT $(cat $PIDF)
echo "Nginx service stop success." 
else
echo "Nginx service already stop"
fi
   ;;
restart)
    $0 stop
    $0 start
    ;;
status)
netstat -anplt |grep ":80" &>/dev/null &&pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
echo "Nginx service is running."
else
echo "Nginx is stop."
fi
  ;; 
reload)
netstat -anplt |grep ":80" &>/dev/null &&pgrep "nginx" &> /dev/null
if [ $? -eq 0 ]
then
    $PROG -t&> /dev/null
if [ $? -eq 0 ] ; then
kill -s HUP $(cat $PIDF)
echo "reload Nginx config success."
else
      $PROG -t
fi
else
echo "Nginx service is not run." 
fi
    ;;
  *)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
[root@hexiaoshuai ~]# chmod +x /etc/init.d/nginx 
[root@hexiaoshuai ~]# chkconfig --add nginx
[root@hexiaoshuai ~]# chkconfig nginx on
[root@hexiaoshuai ~]# service nginx start
Nginx service start success.
[root@hexiaoshuai ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      43798/nginx: master 
[root@hexiaoshuai ~]# firewall-cmd --permanent --add-port=80/tcp
success
[root@hexiaoshuai ~]# firewall-cmd --reload
success

注:如果你想在已安装好的nginx上添加第三方模块,依然需要重新编译,但为了不覆盖你原有的配置,请不要make install,而是直接拷贝可执行文件:

#nginx–V
[root@hexiaoshuai nginx-1.10.2]#./configure--add-module=……   #你的第三方模块
[root@hexiaoshuai nginx-1.10.2]#make后不要makeinstall,改为手动拷贝,先备份
[root@hexiaoshuai nginx-1.10.2]#cp /usr/local/nginx1.10/sbin/nginx/usr/local/nginx1.10/sbin/nginx.bak
[root@hexiaoshuai nginx-1.10.2]#cpobjs/nginx /usr/local/nginx1.10/sbin/nginx

配置nginx反向代理:反向代理+负载均衡+健康探测
查看nginx加载的模块

[root@hexiaoshuai nginx-1.10.2]# nginx -V
nginx version: nginx/1.10.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx1.10 --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre --add-module=../ngx_cache_purge-2.3 --with-http_flv_module --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42

nginx的所有模块必须在编译的时候添加,不能再运行的时候动态加载。
3、nginx-sticky-module模块:
这个模块的作用是通过cookie黏贴的方式将来自同一个客户端(浏览器)的请求发送到同一个后端服务器上处理,这样一定程度上可以解决多个backend servers的session同步的问题 —— 因为不再需要同步,而RR轮询模式必须要运维人员自己考虑session同步的实现。
另外内置的ip_hash也可以实现根据客户端IP来分发请求,但它很容易造成负载不均衡的情况,而如果nginx前面有CDN网络或者来自同一局域网的访问,它接收的客户端IP是一样的,容易造成负载不均衡现象。nginx-sticky-module的cookie过期时间,默认浏览器关闭就过期。

分页阅读: 1 2 3 4 5 6
【声明】:8090安全小组门户(https://www.8090-sec.com)登载此文出于传递更多信息之目的,并不代表本站赞同其观点和对其真实性负责,仅适于网络安全技术爱好者学习研究使用,学习中请遵循国家相关法律法规。如有问题请联系我们:邮箱hack@ddos.kim,我们会在最短的时间内进行处理。