一、简介
通过Nginx的动静分离技术,可以实现将服务器单独只提供一种页面,例如,一台web服务器专门提供图片,css,js等静态资源,另外的一台服务器专门处理例如.php,.jsp等动态处理的请求。这样可以减轻双方服务器的压力,同时又可以做到负载分担。
二、拓扑
三、配置实现
0.开启Nginx的路由转发
#vi /etc/sysctl.conf net.ipv4.ip_forward = 1 #sysctl -p
配置Nginx的动静分离
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#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;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { #响应首页以及非图片等静态资源
proxy_pass http://192.168.112.130;
index index.html index.htm;
}
location ~ \.(jpg|gif|png|css)$ { #响应图片等静态资源
proxy_pass http://192.168.112.131;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}2.配置后端的web服务器,一台提供index.html,1台提供1.jpg,1.gif模拟测试
提供index.html的测试
# cat /var/www/html/index.html message from <img src=1.jpg> <img src=1.gif>
提供1.jpg和1.gif的web服务器
# ls /var/www/html/ 1.jpg 1.gif
3.测试效果
本文出自 “Lu2Yu” 博客,请务必保留此出处http://lu2yu.blog.51cto.com/10009517/1626953
原文地址:http://lu2yu.blog.51cto.com/10009517/1626953