1. 下載軟件包
1.1 下載Nginxjavascript
wget http://nginx.org/download/nginx-1.11.4.tar.gz tar zxvf nginx-1.11.4.tar.gz
1.2 下載緩存清除 模塊 ngx_cache_purgephp
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz tar zxvf ngx_cache_purge-2.3.tar.gz
1.3 下載Nginx Image 縮略圖 模塊 ngx_image_thumb-mastercss
wget https://github.com/3078825/nginx-image/archive/master.zip unzip master.zip
2. 安裝環境依賴包html
yum install autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gd-devel
3. 編譯安裝
3.1 配置
進入nginx解壓目錄,同時注意保持ngx_cache_purge與nginx爲同級目錄(安裝模塊時指定的目錄路徑)java
./configure \ --prefix=/usr/local/nginx-1.11.4 \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_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/ \ --add-module=../ngx_cache_purge-2.3 \ --add-module=../ngx_image_thumb-master
3.2 編譯安裝
node
make & make install
安裝好以後,默認的nginx根目錄爲 /usr/local/nginxnginx
4 配置git
4.1 增長nginx用戶github
groupadd -f nginx useradd -g nginx nginx
4.2 編寫啓動腳本後端
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
4.3 添加腳本運行權限
chmod +x /etc/init.d/nginx
4.4 增長開機啓動
chkconfig --add nginx chkconfig nginx on chkconfig nginx --list
5 配置nginx
5.1 nginx.conf
參考內容:
user www www; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } 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; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; server_name_in_redirect off; client_max_body_size 10m; client_body_buffer_size 128k; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 0; keepalive_timeout 60; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 3; gzip_disable "MSIE [1-6]."; gzip_types text/plain application/x-javascript text/css application/xml image/jpeg image/gif image/png; #添加圖片壓縮 gzip_vary on; gzip_proxied any; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_buffer_size 64k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #傳遞真實ip給後端 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; open_file_cache max=204800 inactive=20s; open_file_cache_min_uses 1; open_file_cache_valid 30s; proxy_cache_path /cache/proxy_cache levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=30g; #100m和30G,按照服務要求,適當增大 proxy_temp_path /cache/proxy_temp; include vhost/*.conf; }
5.2 配置站點文件
upstream tomcat_server { server 192.168.0.222 weight=1 max_fails=2 fail_timeout=30s; #添加ip,不是tomcat的8080端口 server 192.168.0.223 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name 192.168.0.211; index index.html index.jsp index.php; location / { proxy_next_upstream http_503 http_500 http_502 error timeout invalid_header; proxy_cache cache_one; add_header Nginx-Cache "$upstream_cache_status"; proxy_cache_key $host$uri$is_args$args; proxy_set_header Accept-Encoding ""; proxy_pass http://tomcat_server; proxy_cache_valid 200 304 12h; proxy_cache_valid 301 302 1m; proxy_cache_valid any 1m; expires 1d; } #jsp,do文件不進行cache location ~ .*\.(jsp|do)$ { proxy_set_header Accept-Encoding ""; #只添加了一個, 其餘的都添加到主配置文件了,之後添加站點不用在重複寫 proxy_pass http://tomcat_server; } location ~ /purge(/.*) { allow 127.0.0.1; allow 192.168.0.0/24; deny all; proxy_cache_purge cache_one $host$1$is_args$args; } location /ngx_status { stub_status on; access_log off; allow 127.0.0.1; allow 192.168.0.0/24; #本身的ip地址 deny all; } }
參考:
http://blog.csdn.net/kntao/article/details/46790027
http://www.cnblogs.com/kreo/p/4378086.html
http://www.open-open.com/lib/view/open1431254539701.html