2014-04-01 09:53:01
来 源
ITJS.CN
Nginx
本文介绍CentOS6.4系统配置Nginx服务器反向代理和缓存,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
1. 最小化安装 centos 6.2

2. 编译环境前提

# yum -y install gcc gcc-c++ make openssl-devel pcre-devel

# groupadd -g 80 www

# useradd -u 80 -g www -M -s /sbin/nologin www

3. Nginx服务端安装

# wget http://nginx.org/download/nginx-1.2.2.tar.gz

# wget http://labs.frickle.com/files/ngx_cache_purge-1.6.tar.gz

# tar zxvf nginx-1.2.2.tar.gz

# tar zxvf ngx_cache_purge-1.6.tar.gz

# cd nginx-1.2.2

# ./configure --add-module=../ngx_cache_purge-1.6 --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_sub_module && make && make install

4. 开机自启动脚本

# 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/local/nginx/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

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

killall -9 nginx

}

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

# chmod 755 /etc/init.d/nginx

# chkconfig --add nginx

# chkconfig --level 2345 nginx on

配置文件

# vi /usr/local/nginx/conf/nginx.conf

user www www;

worker_processes 8;

error_log logs/error.log crit;

pid logs/nginx.pid;

worker_rlimit_nofile 1024;

events {

use epoll;

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

large_client_header_buffers 4 32k;

client_max_body_size 300m;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 60;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.1;

gzip_comp_level 2;

gzip_types text/plain application/x-javascript text/css application/xml;

gzip_vary on;

#缓存目录设置

proxy_temp_path /cache/proxy_temp_dir;

proxy_cache_path /cache/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=4g;

#负载均衡

upstream backend_server {

server 8.8.4.4:80 weight=1 max_fails=2 fail_timeout=30s;

server 4.4.4.4:80 weight=1 max_fails=2 fail_timeout=30s;

}

#状态监控

server {

listen 84;

server_name _;

location / {

stub_status on;

access_log off;

}

}

#空主机头

server {

listen 80;

server_name 8.8.8.8;

return 404;

}

#代理域名配置

server {

listen 80;

server_name www.server110.com;

index index.html index.htm index.php index.asp;

access_log logs/www.server110.com.log;

#清除缓存配置

location ~ /purge(/.*) {

allow all;

deny 127.0.0.1;

proxy_cache_purge cache_one $host$1$is_args$args;

}

#反向代理

location / {

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_pass http://8.8.8.8:80;

}

#缓存类型

location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {

proxy_next_upstream http_502 http_504 error timeout invalid_header;

proxy_cache cache_one;

proxy_cache_valid 200 304 24h;

proxy_cache_valid 301 302 1m;

proxy_cache_valid any 1m;

proxy_cache_key $host$uri$is_args$args;

proxy_set_header Host  $host;

proxy_set_header X-Forwarded-For $remote_addr;

proxy_pass http://8.8.8.8:80;

add_header X-Cache HIT-PY;

expires 1d;

}

}

}

如果要清理 http://www.server110.com/logo.jpg 这个缓存文件,通过访问http://www.server110.com/purge/logo.jpg就可以清除掉了。

 

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。