一聚教程网:一个值得你收藏的教程网站

热门教程

nginx反向代理配置两个实例

时间:2022-06-30 18:50:52 编辑:袖梨 来源:一聚教程网

例1

目前就稳定性来说,Apache是没得比的。因此,用nginx做反向代理比较合适。
这里是给http://172.30.170.8:8088/vod/做代理,反向代理服务器的名称为vod.xx.xxx.cn ,监听80端口。
Apache httpd服务器监听8088端口(我这里apache与反向代理服务器在同一服务器)。

全局配置参数做些调整:

 代码如下 复制代码

hacklog@hywd:/etc/nginx$ cat nginx.conf
user www-data;
worker_processes  4;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections  2048;
# multi_accept on;
}

http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    gzip  on;
    gzip_disable "MSIE [1-6].(?!.*SV1)";

    server_names_hash_bucket_size 256;
    client_header_buffer_size 256k;
    large_client_header_buffers 4 256k;

#size limits
    client_max_body_size             50m;
    client_body_buffer_size        256k;
    client_header_timeout     3m;
    client_body_timeout 3m;
    send_timeout             3m;
        #参数都有所调整.目的是解决代理过程中出现的一些502 499错误   
    sendfile on;
    tcp_nopush         on;
    keepalive_timeout 120; #参数加大,以解决做代理时502错误
    tcp_nodelay on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
 }

然后是反向代理配置了,有些参数必须调整,如client_max_body_size 不调整的话,通过web上传东西会有问题:

 代码如下 复制代码

hacklog@hywd:/etc/nginx/sites-available$ pwd
/etc/nginx/sites-available
hacklog@hywd:/etc/nginx/sites-available$ cat proxy_local_apache
# You may add here your
# server {
#   ...
# }
# statements for each of your virtual hosts

server {
    listen   80 default;
    server_name  vod.xx.xxx.cn;

    access_log  /var/log/nginx/vod.xx.xxx.cn.access.log;

    location ~ ^/status/ {
        stub_status on;
        access_log off;
    }

    location / {
        proxy_redirect off ;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size 50m;
        client_body_buffer_size 256k;
        proxy_connect_timeout 30;
        proxy_send_timeout 30;
        proxy_read_timeout 60;
        proxy_buffer_size 256k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_temp_file_write_size 256k;
        proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
        proxy_max_temp_file_size 128m;

        proxy_pass    http://172.30.170.8:8088/vod/;
    }

    location /doc {
        root   /usr/share;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

 

#error_page  404  /404.html;

hacklog@hywd:/etc/nginx/sites-available$

nginx日志切割脚本

 代码如下 复制代码

root@hywd:/etc# cat /usr/local/sbin/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/var/log/nginx/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}vod.xx.xxx.cn.access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/vod.xx.xxx.cn.access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /var/run/nginx.pid`

crontab -e
# m h  dom mon dow   command
00 00 * * * /bin/bash    /usr/local/sbin/cut_nginx_log.sh

例2

nginx是一个高性能的web服务器,它也是一个很强的反向代理服务器。我们只要下载源码编译安装配置就可以了。

一、安装Nginx
1、安装所需的PCRE库:

 代码如下 复制代码

cd /tmp
wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.13.tar.gz
tar -zxvf pcre-8.13.tar.gz
cd pcre-8.13
./configure --prefix=/usr
make
make install

2、下载nginx源码安装:

 代码如下 复制代码
cd /tmp
wget -c http://nginx.org/download/nginx-1.0.8.tar.gz
tar -zxvf nginx-1.0.8.tar.gz
cd nginx-1.0.8
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_addition_module --with-http_perl_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module
make
make install

3、建立nginx用户:

 代码如下 复制代码

useradd nginx -s /sbin/nologin -M

4、以下是安装后显示的一些安装路径:

 代码如下 复制代码

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

二、配置nginx

1、编辑配置文件/usr/local/nginx/conf/nginx.conf

 代码如下 复制代码

vim /usr/local/nginx/conf/nginx.conf

2、修改以下内容

将第一行user前的#去掉,改为:

 代码如下 复制代码

user nginx nginx;

将worker_processes改大,比如设为2:

 代码如下 复制代码

worker_processes 2;

将以下两行前的#去掉:

 代码如下 复制代码

error_log logs/error.log
pid logs/nginx.pid

绑定用来代替的域名或IP:

 代码如下 复制代码
listen 80;

server_name 绑定的域名或IP;

3、配置反向代理内容,并使用HttpSubModule替换URL:

找到

 代码如下 复制代码
location / {
root html;
index index.html index.htm;

在后加入:

 代码如下 复制代码

sub_filter 要反向代理的网址 代替的域名或IP; #替换URL
sub_filter_once off; #搜索替换全部行
proxy_pass http://要反向代理的网址;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding ""; #清除编码

3、编写nginx启动脚本,设置开机自启动:

 代码如下 复制代码

vim /etc/init.d/nginx

启动脚本

 代码如下 复制代码

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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"

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
}

restart() {
    configtest || return $?
    stop
    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 --level 345 nginx on

热门栏目