Nginx's powerful response capabilities to static files make up for the shortcomings of the Apache environment to some extent. nginx is used as a reverse proxy, and some of them are used to improve the corresponding capabilities of static files, in addition, nginx can be used for load balancing to build two or three Apache servers. This solves the server resource problems arising from the rapid development of the business of the initial team in a long period of time.
1. Install the dependency. yum-y install wget zip unzip zlib-devel PCRE * OpenSSL-devel Perl-extutils-embed. 2, download the source package wget http://nginx.org/download/nginx-1.6.1.tar.gz 3, unzip tar-zxvf nginx-1.6.1.tar.gz CD nginx-1.6.1/4, create the required directory mkdir/var/tmp/nginx // var/log/nginx // var/run/nginx/5, compile and install it. /configure -- prefix =/usr -- sbin-Path =/usr/sbin/nginx -- conf-Path =/etc/nginx. conf -- error-log-Path =/var/log/nginx/error. log -- PID-Path =/var/run/nginx. PID -- lock-Path =/var/lock/nginx. lock -- http-log-Path =/var/log/nginx/access. log -- 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-http_ssl_module -- with-http_flv_module -- with-http_gzip_static_module -- with-http_realip_module -- with-http_stub_status_module -- with-http_addition_module -- with-http_sub_module -- with-http_dav_module -- with-http_perl_module -- With-LD- opt = "-wl, -e "-- with-mail make & make install 6, create/usr/sbin/useradd-c "nginx user"-S/bin/false-r-D/var/lib/nginx 7 for nginx, create the startup script VI/etc/init. d/nginx:
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=([^ ]*).*/1/g‘ -` options=`$nginx -V 2>&1 | grep ‘configure arguments:‘` for opt in $options; do if [ `echo $opt | grep ‘.*-temp-path‘` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
8. Set the CHMOD 755/etc/init. d/nginx 9. For operations after the system service chkconfig -- add nginx, you can use the systemctl tool of centos7 to start nginx systemctl stop nginx systemctl restart nginx 10, set static/dynamic separation between nginx and Apache. Edit the Apache configuration file and change Listen to port 88 VI/etc/nginx. the conf file is added to and downstream in the first server configuration.
Location/{root/var/www/html; # index. php index.html index.htm of the Apache website root directory;} # distribute PHP file requests to the backend Apache location ~ \. PHP $ {proxy_set_header host $ host; proxy_set_header X-real-IP $ remote_addr; proxy_set_header X-forwarded-for $ scheme; proxy_pass http: // 127.0.0.1: 88 ;} # process images and static files by nginx location ~ \. (HTM | HTML | GIF | JPG | JPEG | PNG | BMP | SWF | IOC | RAR | zip | TXT | FLV | mid | Doc | PPT | PDF | XLS | MP3 | WMA) $ {root/var/www/html; expires 15d ;}# process the JS file by nginx location ~ \. (JS | CSS) $ {expires 1 h ;}
11. Configure nginx Server Load balancer
Add VI/etc/nginx. conf to the HTTP node:
http { include mime.types; default_type application/octet-stream; keepalive_timeout 120; tcp_nodelay on; upstream server1 { server 192.168.1.2:80; server 192.168.1.3:80; server 192.168.1.4:80; server 192.168.1.5:80; } upstream server2 { server 192.168.1.7:8080; server 192.168.1.7:8081; server 192.168.1.7:8082; } server { listen 80; server_name server1; location / { proxy_pass http://www.zyan.cc; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 80; server_name server2; location / { proxy_pass http://blog.zyan.cc; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
In addition, if an error occurs during nginx compilation, the HTTP rewrite module requires the PCRE Library. // install PCRE the HTTP gzip module requires the zlib library // install zlib make [1]: * ** [/usr/local/PCRE/makefile] Error 127 //-with-PCRE = dir points to the source package instead of the package after installation,
Nginx compilation and installation and Apache dynamic/static separation coexistence settings and Server Load balancer settings