Nginx Web server is currently the most widely used in it enterprises, why it is so people love it, because of its high performance, stability, and more and more development, that nginx in the enterprise how to apply it? How to configure Nginx static and dynamic separation, today we will be together with you to build an nginx static and dynamic separation of the architecture.
First, the practical environment:
System version: CentOS6.0 X86_64nginx version: Nginx-1.2.6tomcat version: Tomcat-6.0.18
Second, Nginx installation:
Install Nginx in the actual environment, first need to install Pcre Library, and then install Nginx:
#安装pcre支持rewrite库, you can also install the source code, note * When installing the source code, the specified pcre path to extract the source path, not the compiled path, otherwise it will error.
Yum install pcre-devel pcre-y# download Nginx source package cd/usr/src; Wget-c http://nginx.org/download/nginx-1.2.6.tar.gz# Decompression Nginx Source Package Tar-xzf nginx-1.2.6.tar.gz# into the decompression directory, and then sed modified nginx version information for TDTWSCD 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# pre-compiled nginx./configure--user=www- -group=www--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module#.configure precompiled successfully, Execute make command to compile Make#make after successful execution, do install official installation
#自此Nginx安装完毕!!!
Third, the configuration nginx:
This is because my 51CTO blog has been installed and configured with Tomcat, which is ignored here, only the Nginx configuration.
#进入Nginx应用目录
cd/usr/local/nginx/conf# Backup original nginx.conf file mv nginx.conf Nginx.bak
The
Creates VI nginx.conf and writes the following:
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 logslog_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应用集群配置, according to the actual situation of the backend, TDT_WUGK is the load balancer name, you can specify the # However, you must match the pass segment of the vhosts.conf virtual host, or you cannot forward the back-end request. 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应用负载均衡配置, can be modified according to the actual situation of the backend. Tdt_app is a load balancer name and can be arbitrarily specified 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:8081 weight=1max_fails=2fail_timeout=30s; server 10.10.141.42:8080 weight=1max_fails=2fail_timeout=30s; server 10.10.141.42:8081 weight=1max_fails=2fail_timeout=30s;} #include引用vhosts. conf, the file is primarily used to configure nginx virtual host include vhosts.conf;} If the nginx.conf configuration is complete, continue to configure Nginx virtual host, continue to create vhosts.conf vi vhosts.conf content in the current directory as follows: # # # # Www.wuguangke.cnserver { 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;   &NBSP} #动态页面交给http://TDT_WUGK, also known as the upstream tdt_wugk equilibrium location ~ .*\ we defined earlier in nginx.conf. (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_ #配置Nginx动静分离, the defined static pages are read directly from the Nginx publishing directory (PASS HTTP://TDT_WUGK;   &NBSP). location ~ .*\. (HTML|HTM|GIF|JPG|JPEG|BMP|PNG|ICO|TXT|JS|CSS) $ { root / data/www/wugk; #expires定义用户浏览器缓存的时间为3天, if the static page is not updated frequently, can be set longer, which can save bandwidth and relieve the pressure of the server   EXPIRES      3D;   &NBSP, #定义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;}
Iv. Deployment Testing:
The backend is configured with a good Tomcat service, and launched, the program needs to be synchronized to the Nginx/data/www corresponding directory, because the configuration of static and dynamic separation, the user requests you to define the still page, the default will go to Nginx Publish directory request, and not to the backend request, So at this point you have to ensure that the back end of the program is consistent, you can use rsync for server-side automatic synchronization or use of NFS, MFS distributed shared storage.
#检查Nginx配置文件是否配置正确, indicate OK and successful are correct, as follows:
[Email protected] ~]#/usr/local/nginx/sbin/nginx-t
Nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntax is oknginx:configuration file/usr/local/nginx/ Conf/nginx.conf Test is successful
#启动Nginx服务
/usr/local/nginx/sbin/nginx
#查看Nginx进程是否启动
Ps-ef |grep Nginx
Since then, nginx static and dynamic separation configuration is complete, welcome to communicate together!
Enterprise actual combat nginx static and dynamic separation installation configuration