Nginx动静分离经典案例配置
随着Nginx高性能Web服务器大量被使用,目前Nginx最新稳定版为1.2.6,张宴兄在实际应用中大量使用Nginx,并分享Nginx高性能Web服务器知识,使得Nginx在国内也是飞速的发展。那今天咱们再来温习一下Nginx 动静分离知识,这里仅供参考。
一、实践环境:
- 系统版本:CentOS6.0 X86_64
- Nginx版本:Nginx-1.2.6
- Tomcat版本:Tomcat-6.0.18
二、Nginx安装:
实际环境中安装Nginx,首先需要安装pcre库,然后再安装Nginx:
- #安装pcre支持rewrite库,也可以安装源码,注*安装源码时,指定pcre路径为解压源码的路径,而不是编译后的路径,否则会报错。
- yum install pcre-devel pcre -y
- #下载Nginx源码包
- cd /usr/src ;wget -c http://nginx.org/download/nginx-1.2.6.tar.gz
- #解压Nginx源码包
- tar -xzf nginx-1.2.6.tar.gz
- #进入解压目录,然后sed修改Nginx版本信息为TDTWS
- cd nginx-1.2.6 ; sed -i -e 's/1.2.6//g' -e 's/nginx\//TDTWS/g' -e 's/"NGINX"/"TDTWS"/g' src/core/nginx.h
- #预编译Nginx
- ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
- #.configure预编译成功后,执行make命令进行编译
- make
- #make执行成功后,执行make install 正式安装
- make install
- #自此Nginx安装完毕!!!
三、配置Nginx:
这里鉴于我的51CTO博客已经有Tomcat安装和配置了,这里忽略,只配置Nginx。
- #进入Nginx应用目录
- cd /usr/local/nginx/conf
- #备份原nginx.conf文件
- mv nginx.conf nginx.bak
创建 vi nginx.conf ,并写入如下内容:
- user www www;
- worker_processes 8;
- worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
- pid /usr/local/nginx/nginx.pid;
- worker_rlimit_nofile 102400;
- events
- {
- use epoll;
- worker_connections 102400;
- }
- http
- {
- include mime.types;
- default_type application/octet-stream;
- fastcgi_intercept_errors on;
- charset utf-8;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 4k;
- large_client_header_buffers 4 32k;
- client_max_body_size 300m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- client_body_buffer_size 512k;
- proxy_connect_timeout 5;
- proxy_read_timeout 60;
- proxy_send_timeout 5;
- proxy_buffer_size 16k;
- proxy_buffers 4 64k;
- proxy_busy_buffers_size 128k;
- proxy_temp_file_write_size 128k;
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_http_version 1.1;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- ###2012-12-19 change nginx logs
- log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $request_time $remote_addr';
- #这里为后端服务器wugk应用集群配置,根据后端实际情况修改即可,tdt_wugk为负载均衡名称,可以任意指定
- #但必须跟vhosts.conf虚拟主机的pass段一致,否则不能转发后端的请求。
- upstream tdt_wugk {
- server 10.10.141.30:8080 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.30:8081 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.31:8080 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.31:8081 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.32:8080 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.32:8081 weight=1max_fails=2fail_timeout=30s;
- }
- #这里为后端APP应用负载均衡配置,根据后端实际情况修改即可。tdt_app为负载均衡名称,可以任意指定
- upstream tdt_app {
- server 10.10.141.40:8080 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.40:8081 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.41:8080 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.41:8080 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.44:8080 weight=1max_fails=2fail_timeout=30s;
- server 10.10.141.45:8080 weight=1max_fails=2fail_timeout=30s;
- }
- #include引用vhosts.conf,该文件主要用于配置Nginx 虚拟主机
- include vhosts.conf;
- }
如上nginx.conf配置完毕,继续配置nginx虚拟主机,继续在当前目录创建vhosts.conf
vi vhosts.conf 内容如下:
- ####www.wuguangke.cn
- server
- {
- listen 80;
- server_name www.wuguangke.cn;
- index index.html index.htm;
- #配置发布目录为/data/www/wugk
- root /data/www/wugk;
- location /
- {
- proxy_next_upstream http_502 http_504 error timeout invalid_header;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tdt_wugk;
- expires 3d;
- }
- #动态页面交给http://tdt_wugk,也即我们之前在nginx.conf定义的upstream tdt_wugk 均衡
- location ~ .*\.(php|jsp|cgi)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tdt_wugk;
- }
- #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。
- location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
- {
- root /data/www/wugk;
- #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
- expires 3d;
- }
- #定义Nginx输出日志的路径
- access_log /data/logs/nginx_wugk/access.log main;
- error_log /data/logs/nginx_wugk/error.log crit;
- }
- ##########chinaapp.sinaapp.com 2012-12-19
- server
- {
- listen 80;
- server_name chinaapp.sinaapp.com;
- index index.html index.htm;
- root /data/www;
- location /
- {
- proxy_next_upstream http_502 http_504 error timeout invalid_header;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tdt_app;
- expires 3d;
- }
- location ~ .*\.(php|jsp|cgi)?$
- {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://tdt_app;
- }
- location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
- {
- root /data/www/app;
- expires 3d;
- }
- access_log /data/logs/nginx_app/access.log main;
- error_log /data/logs/nginx_app/error.log crit;
- }
四、部署测试:
后端配置好Tomcat服务,并启动,发布的程序需同步到Nginx的/data/www对应的目录,因为配置动静分离后,用户请求你定义的静态页面,默认会去nginx的发布目录请求,而不会到后端请求,所以这时候你要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步。
- #检查Nginx配置文件是否配置正确,提示Ok and successful表示正确,如下:
- [root@WEB-11-151 ~]# /usr/local/nginx/sbin/nginx -t
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- #启动Nginx服务
- /usr/local/nginx/sbin/nginx
- #查看Nginx进程是否启动
- ps -ef |grep nginx
本文只是一个简单的实际案例,里面的配置和参数这里没有做过多的说明,后期会继续更新。
相关推荐
nginxs 2020-11-14
Guanjs0 2020-11-13
小木兮子 2020-11-11
yserver 2020-11-11
ssihc0 2020-11-11
windle 2020-11-10
HanksWang 2020-11-10
liuchen0 2020-11-10
Freshairx 2020-11-10
ccschan 2020-11-10
liwf 2020-11-10
Guanjs0 2020-11-09
AderStep 2020-11-09
zrtlin 2020-11-09
mqfcu 2020-11-10
windle 2020-10-29