首页 > 代码库 > nginx反向代理(尚未完善)

nginx反向代理(尚未完善)

实例配置:nginx反向代理:
    环境:
        vm1-nginx:
            IP: eth0 172.16.3.2/16
                eth1 192.168.1.2/24
        vm2-apache+php+mysql:
            IP: eth0 192.168.1.3/24
        vm3-apache:
            IP: eth0 192.168.1.10/24

测试本机:IP 172.16.255.109/16

1、vm1-nginx:配置;


# ifconfig eth0 172.16.3.2/16 up
# ifconfig eth1 192.168.1.2/24 up
# yum install nginx


 
# cat /etc/nginx/nginx.conf
#user  nobody;
worker_processes  1;
worker_rlimit_nofile 99999;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log /data/applogs/nginx/error.log notice;          定义错误日志的级别和存储路径
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    gzip on;                                  开启压缩功能
    gzip_http_version 1.0;                        定义http协议版本
    gzip_comp_level 2;                            定义压缩级别
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;  定义压缩那些格式的内容
    gzip_disable msie6;   什么样的浏览器不执行压缩
    upstream webservers {                                  定义代理的后端服务器组
    server 192.16.1.1 weight=2  max_fails=2 fail_timeout=3;          定义backend-server1
    server 192.16.1.10 weight=1 max_fails=2 fail_timeout=3;              定义backend-server2
    server 127.0.0.1:8080 backup;                                    定义本机的备用页面server
    least_conn;                                                    负载均衡的方法,least_conn最少连接
    }

    server {                           虚拟主机www.hong.com
        listen  80 default_server;       
        server_name  www.hong.com;
        root   /www/ning;
        deny 172.16.3.3;                        拒绝172.16.3.3访问www.hong.com
        allow 172.16.0.0/16;
        deny all;   
    }
    server {
        listen 80;
        server_name  www.ning.com;
        root /www/ning;   
        location /admin {                用户验证机制
           auth_basic "admin area";            用户验证的提示信息
           auth_basic_user_file /etc/nginx/.htpasswd;        用户密码存放方式
        }   
        location /download/ {                      定义下载网站
           autoindex on;                            开启下载网站
           rewrite ^/download/(.*\.(jpg|gif|jpeg|png))$ /images/$1 break;     定义重写路径
           rewrite_log on;                                                    开启记录重写日志日志
        }
        location ~* \.(jpg|png|gif|jpg)$ {                      定义防盗链功能
            root /www/ning;
            valid_referers none blocked www.ning.com *.ning.com;    定义允许盗链的规则
            if ($invalid_referer) {                                定义不符合规则的重写到那个页面
                rewrite ^/ http://www.ning.com/403.html;
            }
        }
        location  /server-status {                开启sever:www.ning.com的状态功能
            stub_status on;                    开启状态检测
            access_log off;                    不记录访问日志
            allow 172.16.255.109;            允许172.16.255.109用户访问
            deny all;                        拒绝所有的用户
        }

    }
    server {                                         定义代理内容
        listen 80;
        server_name www.liang.com;                       指定代理的server名称
        location / {
            proxy_pass http://webservers;                     指定代理的url
            proxy_set_header X-Real-IP $remote_addr          重写请求报文头部信息(添加客户端IP)
        }
    }
    server {                                指定备用的server
        listen 8080;
        server_name 127.0.0.1;
        root /www/htdocs;
    }
}           

 

# service nginx start

 

2、vm2-apache+php+mysql:配置


# ifconfifg eth0 192.168.1.3/24 up
# yum install httpd php php-mysql mysql –y

#  mkdir /www/ning –pv

# vim /www/ning/index.php

<?php

phpinfo();

?/>

 

# service httpd start

 

3、vm3-apache:配置

# ifconfig eth0 192.168.1.10/24 up

# yum install httpd

# mkdir /www/ning –pv

# echo “192.168.1.10” > /www/ning/index.html

# service httpd start

 

 

测试:http://www.ning.com (上面设置的是最小连接数)

nginx反向代理(尚未完善)