Maison >développement back-end >tutoriel php >nginx反向代理(及优化)
源码安装nginx
这里装nginx的三个依赖,分别是pcre、openssl、zlib
其中编译pcre需要:
<code>yum install gcc gcc<span>-c</span><span>++</span> pcre<span>-devel</span></code>
下载源码包
<code>官网下载最新版即可: <span>http:</span>//www<span>.pcre</span><span>.org</span>/ <span>http:</span>//www<span>.openssl</span><span>.org</span><span>http:</span>//www<span>.zlib</span><span>.net</span>/ <span>http:</span>//nginx<span>.org</span></code>
注意:这里pcre只能是是8.0+,pcre2不支持
会报错:
<code>make[2]: *** No rule to make target <span>`libpcre.la'</span>. Stop.</code>
除了pcre我都用的最新稳定版,给个我用的pcre源码包:
<code><span>ftp:</span>//ftp<span>.csx</span><span>.cam</span><span>.ac</span><span>.uk</span>/pub/software/programming/pcre/pcre-<span>8.36</span><span>.tar</span><span>.gz</span></code>
编译
这里不用分别编译安装,直接进入解压的nginx目录下执行
假设文件都放在/home目录
<code>./configure --prefix=/data/nginx -<span>-with-http_realip_module \ </span>-<span>-with-http_sub_module \ </span>-<span>-with-http_flv_module \ </span>-<span>-with-http_dav_module \ </span>-<span>-with-http_gzip_static_module \ </span>-<span>-with-http_stub_status_module \ </span>-<span>-with-http_addition_module \ </span>-<span>-with-pcre=<span>/home/pcre</span>2-<span>10.00</span> \ </span>-<span>-with-openssl=<span>/home/openssl</span>-<span>1.0</span>.<span>2</span>a \ </span>-<span>-with-http_ssl_module \ </span>-<span>-with-zlib=<span>/home/zlib</span>-<span>1.2</span>.<span>8</span></span></code>
注意绿色的三个是指定源码的目录,不是安装目录,因为本方法是联合编译的,不需要提前编译安装pcre,ssl,zlib
然后就是:
<code><span>make</span><span>make</span> install</code>
执行
按照上面的安装方法,nginx装在/data/nginx
<code>./<span><span>data</span>/nginx/sbin/nginx -c /<span>data</span>/nginx/conf/nginx.conf</span><span>#因为它需要指定配置文件才能运行,执行这条配置文件没有返回,建议使用脚本控制</span></code>
脚本如下
<code><span>#!/bin/sh</span><span># config: /usr/local/nginx/conf/nginx.conf</span>nginx_path=<span>"/data/nginx"</span> nginx_pid=<span>"/data/nginx/logs/nginx.pid"</span><span># Source function library.</span> . /etc/rc.d/init.d/functions <span># Source networking configuration.</span> . /etc/sysconfig/network <span># Check that networking is up.</span> [ <span>${NETWORKING}</span> = <span>"no"</span> ] && <span>exit</span><span>0</span>[ -x <span>$nginx_path</span>/sbin/nginx ] || <span>exit</span><span>0</span>RETVAL=<span>0</span> prog=<span>"nginx"</span><span><span>start</span></span>() { <span># Start daemons.</span><span>if</span> [ <span>-e</span><span>$nginx_pid</span><span>-a</span> ! -z <span>$nginx_pid</span> ];<span>then</span><span>echo</span><span>"nginx already running...."</span><span>exit</span><span>1</span><span>fi</span><span>if</span> [ <span>-e</span><span>$nginx_path</span>/conf/nginx.conf ];<span>then</span><span>echo</span> -n $<span>"Starting <span>$prog</span>: "</span><span>$nginx_path</span>/sbin/nginx -c <span>$nginx_path</span>/conf/nginx.conf & RETVAL=$? [ <span>$RETVAL</span><span>-eq</span><span>0</span> ] && { touch /var/lock/subsys/<span>$prog</span> success $<span>"<span>$prog</span>"</span> } <span>echo</span><span>else</span> RETVAL=<span>1</span><span>fi</span><span>return</span><span>$RETVAL</span> } <span># Stop daemons.</span><span><span>stop</span></span>() { <span>echo</span> -n $<span>"Stopping <span>$prog</span>: "</span> killproc <span>-d</span><span>10</span><span>$nigx_path</span>/sbin/nginx RETVAL=$? <span>echo</span> [ <span>$RETVAL</span> = <span>0</span> ] && rm <span>-f</span><span>$nginx_pid</span> /var/lock/subsys/<span>$prog</span> } <span># See how we were called.</span><span>case</span><span>"<span>$1</span>"</span><span>in</span> start) start ;; stop) stop ;; reconfigure) stop start ;; status) status <span>$prog</span> RETVAL=$? ;; *) <span>echo</span> $<span>"Usage: <span>$0</span> {start|stop|reconfigure|status}"</span><span>exit</span><span>1</span><span>esac</span><span>exit</span><span>$RETVAL</span></code>
如果脚本名字叫nginx.sh
那么可以:
<code>./nginx.sh status<span>|stop|start....</span></code>
代理的配置
以上就介绍了nginx反向代理(及优化),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。