码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx 反向代理 动静分离

时间:2015-09-02 19:07:31      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:nginx 反向代理

技术分享

1、实验环境:
机器
10.0.10.8  Nginx proxy
10.0.10.12 Nginx静态
10.0.10.10 Ngins动态,LNMP平台,有个Tomcat服务
系统版本和内核
# cat /etc/redhat-release
CentOS release 6.6 (Final)
# uname -r
2.6.32-504.3.3.el6.x86_64

2、Nginx静态服务器的配置文件
# cat /application/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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 {
        listen       80;
        server_name  www.cui.com;
        location / {
            root   /data/www;
            index  index.html index.htm;
        }
        }
}
简单的网页内容以及图片
# cat /data/www/index.html
<html>
<head>test</head>
<body>
This is static site!!
</body>
</html>
# ls /data/www/
1.png  images  index.html  tomcat.png

3、Nginx动态服务器配置文件
# cat /application/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    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 {
        listen       80;
        server_name  www.cui.com;
            root   /data/www;
        location / {
            root   /data/www;
            index index.php index.html index.htm;
        }
        location ~ .*\.(php|php5)?$ {      
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }  
        }
}

简单的网页内容
# cat /data/www/index.php
<?php
echo "This is php dynamic site !!\n";
?>

4、Nginx proxy服务器配置文件
# cat /application/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server_tokens off;
    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;
    include proxy.conf; #proxy一些优化

    upstream static_pools {
        server 10.0.10.12:80 weight=1 max_fails=10 fail_timeout=10s;
        }
    upstream nginx_pools {
        server 10.0.10.10:80 weight=1 max_fails=10 fail_timeout=10s;
        }
    upstream tomcat_pools {
        server 10.0.10.10:8080 weight=1 max_fails=10 fail_timeout=10s;
        }

    server {
        listen       80;
        server_name  www.cui.com;

        #location / {
        #    index index.html index.htm;
        #    proxy_pass http://static_pools; #这里可以设置,也可以不设置,设置成静态默认就找静态,设置成动态默认就找动态
        #}

        location   ~* \.(html|js|css|gif|jpg|jpeg|png|bmp|swf)$ {
             proxy_pass http://static_pools;
             }

        location ^~ /images/ {
             proxy_pass http://static_pools;
                 }

        location ~ .*.(php|cgi|jhtml)$ {
             proxy_pass http://nginx_pools;
              }

        location ~ .*.(jsp)$ {
             proxy_pass http://tomcat_pools;
              }
           }
}
proyx优化的配置文件
# cat /application/nginx/conf/proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

5、验证效果

1.以下这个是默认访问到的静态网页

技术分享

2.以下这个是访问到的以png结尾的静态图片

技术分享

3.以下这个是访问到的images目录的静态图片

技术分享

4.以下是访问以php结尾的动态网页

技术分享


5.以下是访问tomcat服务器jsp网页

技术分享


本文出自 “肖海” 博客,请务必保留此出处http://eveday.blog.51cto.com/10577430/1690810

Nginx 反向代理 动静分离

标签:nginx 反向代理

原文地址:http://eveday.blog.51cto.com/10577430/1690810

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!