您的位置:首页 > 运维架构 > 反向代理

Nginx 反向代理 动静分离

2015-09-08 00:38 525 查看


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网页


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息