In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to build a CDN server for Nginx reverse proxy and proxy_cache cache. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Encounter a problem:
Mobile users are slow to access web server www.osyunwei.com
Solution:
1. Place a nginx reverse proxy server in the mobile computer room
2. Through the intelligent resolution of domain name DNS, all mobile users are resolved to nginx reverse proxy server when accessing www.osyunwei.com.
3. The direct connect between the nginx reverse proxy server and the web server
Description:
1. Web server
Lines: telecommunications
IP:192.168.21.129
Domain name: www.osyunwei.com
2. Nginx reverse proxy server
Lines: Movin
System: CentOS 6.2
IP:192.168.21.164
Edit vi / etc/hosts # and add the following line at the end of the file
192.168.21.129 www.osyunwei.com
3. Client
Lines: Movin
System: Windows 7
IP:192.168.21.130
C:\ Windows\ System32\ drivers\ etc\ hosts # Open with notepad and add the following line at the end of the file
192.168.21.164 www.osyunwei.com
# configure the following actions on the nginx reverse proxy server #
1. Close SELinux
Vi / etc/selinux/config
# SELINUX=enforcing # comment out
# SELINUXTYPE=targeted # comment out
SELINUX=disabled # increased
: wq save, close.
Shutdown-r now restart the system
2. Open port 80 of the firewall
Vi / etc/sysconfig/iptables
Add the following
-An INPUT-m state-- state NEW-m tcp-p tcp-- dport 80-j ACCEPT
/ etc/init.d/iptables restart # restart the firewall for the configuration to take effect
3. Install compilation tools
System operation and maintenance www.osyunwei.com warm reminder: qihang01 original content ©all rights reserved, reprint please indicate the source and the original link
Yum install wget make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl
4. System agreement
Software source code package location: / usr/local/src
Source package compilation installation location: / usr/local/ software name
5. Download the software
Cd / usr/local/src # enter the directory
Download nginx (currently stable version)
Wget http://nginx.org/download/nginx-1.0.12.tar.gz
(2) download pcre (supports nginx pseudo-static)
Wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
Download ngx_cache_purge (clear the specified URL cache)
Wget http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz
6. Install pcre
Cd / usr/local/src
Mkdir / usr/local/pcre # create installation directory
Tar zxvf pcre-8.21.tar.gz
Cd pcre-8.21
. / configure-- prefix=/usr/local/pcre # configuration
Make
Make install
7. Install nginx
Groupadd www # add www Group
Useradd-g www www-s / bin/false # create nginx running account www and join the www group. Www users are not allowed to log in directly to the system.
Cd / usr/local/src
Tar zxvf ngx_cache_purge-1.5.tar.gz
Tar zxvf nginx-1.0.12.tar.gz
Cd nginx-1.0.12
/ configure-- prefix=/usr/local/nginx-- user=www-- group=www-- with-http_stub_status_module-- with-openssl=/usr/-- with-pcre=/usr/local/src/pcre-8.21-- add-module=../ngx_cache_purge-1.5
Note:-- with-pcre=/usr/local/src/pcre-8.21 points to the path where the source package is decompressed, not the path of installation, otherwise an error will be reported.
Make # compilation
Make install # installation
/ usr/local/nginx/sbin/nginx # launch nginx
Chown www.www-R / usr/local/nginx/html # sets the directory owner
Chmod 700R / usr/local/nginx/html # set directory permissions
Vi / etc/rc.d/init.d/nginx # set nginx to start, edit the startup file and add the following
=
#! / bin/bash# nginx Startup script for the Nginx HTTP Server# it is v.0.0.2 version.# chkconfig:-85 "description: Nginx is a high-performance web and proxy server.# It has a lot of features But it's not for everyone.# processname: nginx# pidfile: / var/run/nginx.pid# config: / usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/usr/local/nginx/logs/nginx.pidRETVAL=0prog= "nginx" # Source function library.. / etc/rc.d/init.d/functions# Source networking configuration.. / etc/sysconfig/network# Check that networking is up. [${NETWORKING} = "no"] & & exit 0 [- x $nginxd] | | exit nginx_pid Start nginx daemons functions.start () {if [- e $nginx_pid]; then echo "nginx already running...." Exit 1fi echo-n $"Starting $prog:" daemon $nginxd-c ${nginx_config} RETVAL=$? Echo [$RETVAL= 0] & & touch / var/lock/subsys/nginx return $RETVAL} # Stop nginx daemons functions.stop () {echo-n $"Stopping $prog:" killproc $nginxd RETVAL=$? Echo [$RETVAL= 0] & & rm-f / var/lock/subsys/nginx / usr/local/nginx/logs/nginx.pid} reload () {echo-n $"Reloading $prog:" # kill-HUP `cat ${nginx_pid} `killproc $nginxd-HUP RETVAL=$? Echo} # See how we were called.case "$1" instart) start;; stop) stop;; reload) reload;; restart) stop start;; status) status $prog RETVAL=$?;; *) echo $"Usage: $prog {start | stop | restart | reload | status | help}" exit 1esacexit $RETVAL
=
: wq! Save exit
Chmod 775 / etc/rc.d/init.d/nginx # gives file execution permissions
Chkconfig nginx on # set boot up
/ etc/rc.d/init.d/nginx restart
Service nginx restart
8. Configure nginx
Cp / usr/local/nginx/conf/nginx.conf / usr/local/nginx/conf/nginx.confbak # backup nginx configuration file
(1) set up the nginx running account
Vi / usr/local/nginx/conf/nginx.conf # editing
Find the user nobody; and modify it to
User www www; # on the first line
(2) prohibition of nginx empty mainframe head
Vi / usr/local/nginx/conf/nginx.conf # editing
Find server and add the following to the above line:
# #
Server {
Listen 80 default
Server_name _
Location / {
Root html
Return 404
}
Location ~ / .ht {
Deny all
}
}
# #
/ etc/rc.d/init.d/nginx restart # restart nginx
After this setting, the empty host header access will jump directly to the nginx404 error page.
(3) add nginx virtual host include file
Cd / usr/local/nginx/conf/ # enter the nginx installation directory
Mkdir vhost # create a virtual directory
Vi / usr/local/nginx/conf/nginx.conf # editing
Find the code you added in the previous step, and add the following at the end:
Include vhost/*.conf
For example:
# #
Server {
Listen 80 default
Server_name _
Location / {
Root html
Return 404
}
Location ~ / .ht {
Deny all
}
}
Include vhost/*.conf
# #
(4) add proxy_cache parameter configuration include file
Cd / usr/local/nginx/conf/ # enter the directory
Touch proxy.conf # create the file
Vi / usr/local/nginx/conf/nginx.conf # editing
Find http {add a line below
Include proxy.conf
(5) add the file included in the list of proxied servers
Cd / usr/local/nginx/conf/ # enter the directory
Touch mysvrhost.conf # create the file
Vi / usr/local/nginx/conf/nginx.conf # editing
Find the code you added in the previous step and add a line below
Include mysvrhost.conf
(6). Set nginx global parameters
Vi / usr/local/nginx/conf/nginx.conf # editing
Worker_processes 2; # number of worker processes, which is or twice the number of cores of CPU
Events
{
Use epoll; # increased
Worker_connections 65535; # modified to 65535, maximum number of connections.
}
# the following code adds and modifies the http {section #
Server_names_hash_bucket_size 128; # increased
Client_header_buffer_size 32k; # increased
Large_client_header_buffers 4 32k; # increased
Client_max_body_size 300m; # added
Tcp_nopush on; # changed to on
Keepalive_timeout 60; # modified to 60
Tcp_nodelay on; # increased
Server_tokens off; # added and does not display nginx version information
Gzip on; # changed to on
Gzip_min_length 1k; # increased
Gzip_buffers 4 16k; # increased
Gzip_http_version 1.1; # increased
Gzip_comp_level 2; # increased
Gzip_types text/plain application/x-javascript text/css application/xml; # increased
Gzip_vary on; # increased
(7) setting proxy_cache parameter configuration
Cd / home # enter the directory
Mkdir-p / home/proxy_temp_dir # proxy_temp_dir and proxy_cache_dir must be in the same partition
Mkdir-p / home/proxy_cache_dir # proxy_cache_dir and proxy_temp_dir must be in the same partition
Chown www.www-R proxy_cache_dir proxy_temp_dir # sets the directory owner
Chmod-R 777 proxy_cache_dir proxy_temp_dir # set directory permissions
System operation and maintenance www.osyunwei.com warm reminder: qihang01 original content ©all rights reserved, reprint please indicate the source and the original text chain
Cd / usr/local/nginx/conf/ # enter the directory
Edit vi proxy.conf # to add the following code
Proxy_temp_path / home/proxy_temp_dir; # specifies the temporary file directory
Proxy_cache_path / home/proxy_cache_dir levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g
# set the name of the Web cache to cache_one, the memory cache to 50MB, automatically clear the files that have not been accessed within 1 day, and the hard disk cache to 1GB.
Client_body_buffer_size 512k; # increase the maximum number of bytes requested by the buffer proxy buffer client
Proxy_connect_timeout 60; # increases the timeout for connecting to the back-end server
Proxy_read_timeout 60; # increase the response request timeout of the back-end server
Proxy_send_timeout 60; # increase the timeout for sending data from the back-end server
Proxy_buffer_size 32k; # increase the size of the proxy request cache
Proxy_buffers 4 64k; # increased
Proxy_busy_buffers_size 128k; # increase the size of proxy_buffers that can be applied for when the system is busy
Proxy_temp_file_write_size 128k; # increase the size of temporary files in proxy cache
Proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; # added failover. If the backend server returns errors such as 502,504 or execution timeout, the request will be automatically forwarded to another server in the upstream load balancer pool to achieve failover. Proxy_cache cache_one; # increased use of web cache cache_one
(8) set the list of files of the proxied server
Cd / usr/local/nginx/conf/ # enter the directory
Edit vi mysvrhost.conf # to add the following code
Upstream osyunweihost {
Server 192.168.21.129:80 weight=1 max_fails=2 fail_timeout=30s
}
(9) create a new virtual host configuration file
Cd / usr/local/nginx/conf/vhost # enter the virtual host directory
Touch www.osyunwei.com.conf # build virtual host configuration file
Vi www.osyunwei.com.conf # Editing
Server {listen 80; server_name www.osyunwei.com osyunwei.com; location / {proxy_pass http://osyunweihost; proxy_cache_key $host$uri$is_args$args; # increases the key value to set the web cache. Nginx stores the cache proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_cache_valid 200 304 12h according to the key value md5 hash; expires 2d } location ~. *\. (php | jsp | cgi | asp | aspx | flv | swf | xml)? the extension files listed in $# are not cached. {proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://osyunweihost;} access_log off;} location ~ / purge (/. *) # is used to clear the cache {allow 127.0.0.1; allow 192.168.21.0 # setting allows only the specified IP or IP segments to clear the URL cache. Deny all; proxy_cache_purge cache_one $host$1 $is_args$args;}
# the above operations are configured on the nginx reverse proxy server #
9. Instructions for using ngx_cache_pure clear cache module
Note: according to the configuration, only hosts that allow 192.168.21.0 IP to clear the URL cache can be cleared. Now the client IP I use is: 192.168.21.130, which has permission to clear the URL cache.
1. Browse the picture file: http://www.osyunwei.com/images/nopic.gif
2. Clear the file cache: http://www.osyunwei.com/purge/images/nopic.gif
Hint: Successful purge, cache file cleared successfully. If this file has not been cached, prompt: 404 Not Found
Note:
1. Purge is the ngx_cache_pure module instruction
2. Images/nopic.gif is the URL path of the cache file to be cleared.
This completes the tutorial on configuring a CDN server using the Nginx reverse proxy and proxy_cache caching features.
Attachment:
1. Nginx configuration file / usr/local/nginx/conf/nginx.conf
User www www;worker_processes 2 is responsible for errorship log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events {use epoll; worker_connections 65535;} http {include proxy.conf; include mysvrhost.conf; 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; server_names_hash_bucket_size 128; client_header_buffer_size 32k Large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 60; tcp_nodelay on; server_tokens off; 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; server {listen 80 default; server_name _; location / {root html; return 404;} location ~ / .ht {deny all;}} include vhost/*.conf;}
2. Proxied server list file / usr/local/nginx/conf/mysvrhost.conf
Upstream osyunweihost {
Server 192.168.21.129:80 weight=1 max_fails=2 fail_timeout=30s
}
3. Proxy_cache parameter configuration file / usr/local/nginx/conf/proxy.conf
Proxy_temp_path / home/proxy_temp_dir;proxy_cache_path / home/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;client_body_buffer_size 512kmit. proxycards connects.timeout 60% proxyroomreadytimeout 60% proxyroomsends.timeout 60% proxyroombufferbuffersize 32k.proxycake buffers 4 64k.proxycake buffers busybags buffer sizes 128k proxyroomtempies fileboxes writeboxes size 128k Proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;proxy_cache cache_one
4. Virtual host configuration file / usr/local/nginx/conf/vhost/www.osyunwei.com.conf
Server {listen 80; server_name www.osyunwei.com osyunwei.com; location / {proxy_pass http://osyunweihost; proxy_cache_key $host$uri$is_args$args; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_cache_valid 200 304 12h; expires 2d;} location ~ / purge (/. *) {allow 127.0.0.1 Allow 192.168.21.0 location 24; deny all; proxy_cache_purge cache_one $host$1 $is_args$args;} location ~. *\. (php | jsp | cgi | asp | aspx | flv | swf | xml)? ${proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://osyunweihost;} access_log off;}
Extended reading:
#
Information such as modified version of nginx
Vi / usr/local/src/nginx-1.0.12/src/core/nginx.h # pre-compilation editing
# define nginx_version
# define NGINX_VERSION
# define NGINX_VER
# define NGINX_VAR
Modify the above information to change the nginx display version.
Vi / usr/local/src/http/ngx_http_special_response.c # pre-compilation editing
Static u_char ngx_http_error_full_tail [] =
Static u_char ngx_http_error_tail [] =
Change the above information to your own.
#
This is the end of this article on "how to build a CDN server for Nginx reverse proxy and proxy_cache cache". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.