Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Nginx reverse proxy and proxy_cache cache to build CDN server

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use Nginx reverse proxy and proxy_cache cache to build CDN server", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Nginx reverse proxy and proxy_cache cache to build CDN server"!

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

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 15

# 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.conf

Nginxd=/usr/local/nginx/sbin/nginx

Nginx_config=/usr/local/nginx/conf/nginx.conf

Nginx_pid=/usr/local/nginx/logs/nginx.pid

Retval=0

Prog= "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 0

# start nginx daemons functions.

Start () {

If [- e $nginx_pid]; then

Echo "nginx already running...."

Exit 1

Fi

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" in

Start)

Start

Stop)

Stop

Reload)

Reload

Restart)

Stop

Start

Status)

Status $prog

Retval=$?

*)

Echo $"usage: $prog {start | stop | restart | reload | status | help}"

Exit 1

Esac

Exit $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, and nginx stores the cache according to the key value md5 hash

Proxy_set_header host $host

Proxy_set_header x-forwarded-for $remote_addr

Proxy_cache_valid 200 304 12h

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 (/. *) # used to clear the cache

{

Allow 127.0.0.1

The allow 192.168.21.0 allow 24; # setting allows only the specified ip or ip segment 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; # error_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 512k; proxy_connect_timeout 60; proxy_read_timeout 60; proxy_send_timeout 60; proxy_buffer_size 32k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_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 allow 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.

Thank you for reading, the above is the content of "how to use Nginx reverse proxy and proxy_cache cache to build CDN server". After the study of this article, I believe you have a deeper understanding of how to use Nginx reverse proxy and proxy_cache cache to build CDN server. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report