亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Operation and Maintenance Nginx How to use Nginx reverse proxy and proxy_cache cache to build a CDN server

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

May 12, 2023 pm 05:43 PM
nginx cdn proxy_cache

Encountered a problem:
It is very slow for mobile users to access the web server www.osyunwei.com
Solution:
1. Place a computer in the mobile computer room Taiwan nginx reverse proxy server
2. Through domain name dns intelligent resolution, all mobile users will resolve to nginx reverse proxy server when accessing www.osyunwei.com
3. Use nginx reverse proxy server and web server to Dedicated line connection
Description:
1. Web server
Line: Telecom
ip: 192.168.21.129
Domain name: www.osyunwei.com
2. nginx Reverse proxy server
Line: Mobile
System: centos 6.2
ip: 192.168.21.164
vi /etc/hosts #Edit, add the following line at the end of the file
192.168.21.129 www. osyunwei.com
3. Client
line: mobile
system: windows 7
ip:192.168.21.130
c:\windows\system32\drivers\etc\hosts #Use notepad Open it and add the following line at the end of the file
192.168.21.164 www.osyunwei.com


############ The following operations are configured on the nginx reverse proxy server#########################


1. Turn off selinux

vi /etc/selinux/config
#selinux=enforcing #Comment out
#selinuxtype=targeted #Comment out
selinux =disabled #Add
:wq Save and close.
shutdown -r now restart the system
2. Open firewall port 80
vi /etc/sysconfig/iptables
Add the following content
-a input -m state --state new -m tcp -p tcp --dport 80 -j accept
/etc/init.d/iptables restart #Restart the firewall to make the configuration take effect
3. Install the compilation tool
yum install wget make gcc gcc-c zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl
4, system convention
Software source code package storage location:/usr /local/src
Source code package compilation and installation location:/usr/local/Software name
5. Download software
cd /usr/local/src #Enter the directory
( 1) Download nginx (current 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
(2) 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 to the system directly
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 code package is decompressed. Instead of the installation path, otherwise an error will be reported
make #Compile
make install #Install
/usr/local/nginx/sbin/nginx #Start nginx
chown www.www -r /usr/ local/nginx/html #Set directory owner
chmod 700 -r /usr/local/nginx/html #Set directory permissions
vi /etc/rc.d/init.d/nginx #Settings Start nginx, edit the startup file and add the following content
==================================== =====================
#!/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 and exit
chmod 775 /etc/rc.d/init.d/nginx #Assign File execution permissions
chkconfig nginx on #Set startup
/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 #Back up nginx configuration file
(1) Set nginx running account
vi /usr/local /nginx/conf/nginx.conf #Edit
Find user nobody; change to
user www www; #On the first line
(2), prohibit nginx empty host header
vi /usr/ local/nginx/conf/nginx.conf #Edit
Find the server and add the following content 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 setting like this, empty host header access will jump directly to the nginx404 error page.
(3) Add the 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 #Edit
Find the code added in the previous step and add the following content 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 directory
touch proxy.conf #Create file
vi /usr/local/nginx/conf/nginx.conf #Edit
Find http { Add a line below
include proxy .conf;
(5). Add the proxy server list containing files
cd /usr/local/nginx/conf/ #Enter the directory
touch mysvrhost.conf #Create the file
vi /usr/ local/nginx/conf/nginx.conf #Edit
Find the code 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 #Edit
worker_processes 2; #The number of worker processes is the number of cores of the cpu or twice
events
{
use epoll; #Increase
worker_connections 65535; #Modify to 65535, the maximum number of connections. ###}################The following code is added and modified in the http { part################server_names_hash_bucket_size 128; # Increase ###client_header_buffer_size 32k; #Add ###large_client_header_buffers 4 32k; #Add ###client_max_body_size 300m; #Add ###tcp_nopush on; #Modify to on###keepalive_timeout 60; #Modify to 60###tcp_nodelay on; #Add ###server_tokens off; #Add, do not display nginx version information ###gzip on; #Modify to on###gzip_min_length 1k; #Add ###gzip_buffers 4 16k; #Add ###gzip_http_version 1.1 ; #Add ###gzip_comp_level 2; #Add ###gzip_types text/plain application/x-javascript text/css application/xml; #Add ###gzip_vary on; #Add ### (7), set proxy_cache parameters Configuration ###cd /home #Enter directory ###mkdir -p /home/proxy_temp_dir #proxy_temp_dir and proxy_cache_dir The two folders must be in the same partition ###mkdir -p /home/proxy_cache_dir #proxy_cache_dir and proxy_temp_dir The folders must be in the same partition ###chown www.www -r proxy_cache_dir proxy_temp_dir #Set 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. Please indicate the source and original text link when reprinting ###cd /usr/local/nginx/conf/ #Enter the directory ###vi proxy.conf #Edit and add the following code ###proxy_temp_path /home/proxy_temp_dir; #Specify 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 web cache area name to cache_one and the memory cache to 50mb, automatically clear files that have not been accessed within 1 day, and the hard disk cache is 1gb. ###client_body_buffer_size 512k; #Increase the maximum number of bytes that the buffer proxy can buffer client requests###proxy_connect_timeout 60; #Increase the timeout for connecting to the backend server###proxy_read_timeout 60; #Increase the timeout for the backend server to respond to the request# ##proxy_send_timeout 60; #Increase the timeout time for the backend server to send data ###proxy_buffer_size 32k; #Increase the proxy request buffer size ###proxy_buffers 4 64k; #Increase ###proxy_busy_buffers_size 128k; #Increase the amount that can be applied when the system is busy proxy_buffers size###proxy_temp_file_write_size 128k; #Increase the size of proxy cache temporary files###proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; #增加故障轉(zhuǎn)移,如果后端的服務器返回502、504、執(zhí)行超時等錯誤,自動將請求轉(zhuǎn)發(fā)到upstream負載均衡池中的另一臺服務器,實現(xiàn)故障轉(zhuǎn)移。proxy_cache cache_one; #增加使用web緩存區(qū)cache_one
(八)、設(shè)置被代理服務器文件列表
cd /usr/local/nginx/conf/ #進入目錄
vi mysvrhost.conf #編輯,添加以下代碼
upstream osyunweihost {
server 192.168.21.129:80 weight=1 max_fails=2 fail_timeout=30s;
}
(九)、新建虛擬主機配置文件
cd /usr/local/nginx/conf/vhost #進入虛擬主機目錄
touch www.osyunwei.com.conf #建立虛擬主機配置文件
vi 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; #增加設(shè)置web緩存的key值,nginx根據(jù)key值md5哈希存儲緩存
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)?$ #列出的擴展名文件不緩存。

{
proxy_set_header host $host;
proxy_set_header x-forwarded-for $remote_addr;
proxy_pass http://osyunweihost;
}
access_log off;
}

location ~ /purge(/.*) #用于清除緩存
{
allow 127.0.0.1;
allow 192.168.21.0/24; #設(shè)置只允許指定的ip或ip段才可以清除url緩存。
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
###################以上操作在nginx反向代理服務器上配置###################
9、ngx_cache_pure清除緩存模塊使用說明
說明:根據(jù)配置只允許192.168.21.0/24 ip段的主機才可以清除url緩存,現(xiàn)在我使用的客戶機ip是:192.168.21.130,有權(quán)限清除url緩存。

1、瀏覽圖片文件:http://www.osyunwei.com/images/nopic.gif

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

2、清除這個文件緩存:http://www.osyunwei.com/purge/images/nopic.gif

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

提示:successful purge,緩存文件清除成功,如果這個文件沒有被緩存過,則提示:404 not found

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

備注:
1、purge是ngx_cache_pure 模塊指令
2、images/nopic.gif 是要清除的緩存文件url路徑

至此,使用nginx反向代理和proxy_cache緩存功能配置cdn服務器教程結(jié)束。

附件:

1、nginx配置文件/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、被代理服務器列表文件/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參數(shù)配置文件/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、虛擬主機配置文件/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/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; 
}

擴展閱讀:
#################################################################
nginx修改版本等信息
vi /usr/local/src/nginx-1.0.12/src/core/nginx.h #編譯前編輯
#define nginx_version
#define nginx_version
#define nginx_ver
#define nginx_var
修改上面的信息,即可更改nginx顯示版本。
vi /usr/local/src/http/ngx_http_special_response.c #編譯前編輯
static u_char ngx_http_error_full_tail[] =
static u_char ngx_http_error_tail[] =
修改上面的信息為你自己的。

The above is the detailed content of How to use Nginx reverse proxy and proxy_cache cache to build a CDN server. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
NGINX and Apache: Understanding the Key Differences NGINX and Apache: Understanding the Key Differences Apr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

How to execute php code after writing php code? Several common ways to execute php code How to execute php code after writing php code? Several common ways to execute php code May 23, 2025 pm 08:33 PM

PHP code can be executed in many ways: 1. Use the command line to directly enter the "php file name" to execute the script; 2. Put the file into the document root directory and access it through the browser through the web server; 3. Run it in the IDE and use the built-in debugging tool; 4. Use the online PHP sandbox or code execution platform for testing.

After installing Nginx, the configuration file path and initial settings After installing Nginx, the configuration file path and initial settings May 16, 2025 pm 10:54 PM

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

How to limit user resources in Linux? How to configure ulimit? How to limit user resources in Linux? How to configure ulimit? May 29, 2025 pm 11:09 PM

Linux system restricts user resources through the ulimit command to prevent excessive use of resources. 1.ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Use the ulimit command directly for temporary modification, such as ulimit-n2048, but it is only valid for the current session. 3. For permanent effect, you need to modify /etc/security/limits.conf and PAM configuration files, and add sessionrequiredpam_limits.so. 4. The systemd service needs to set Lim in the unit file

What are the Debian Nginx configuration skills? What are the Debian Nginx configuration skills? May 29, 2025 pm 11:06 PM

When configuring Nginx on Debian system, the following are some practical tips: The basic structure of the configuration file global settings: Define behavioral parameters that affect the entire Nginx service, such as the number of worker threads and the permissions of running users. Event handling part: Deciding how Nginx deals with network connections is a key configuration for improving performance. HTTP service part: contains a large number of settings related to HTTP service, and can embed multiple servers and location blocks. Core configuration options worker_connections: Define the maximum number of connections that each worker thread can handle, usually set to 1024. multi_accept: Activate the multi-connection reception mode and enhance the ability of concurrent processing. s

NGINX's Purpose: Serving Web Content and More NGINX's Purpose: Serving Web Content and More May 08, 2025 am 12:07 AM

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

Nginx Troubleshooting: Diagnosing and Resolving Common Errors Nginx Troubleshooting: Diagnosing and Resolving Common Errors May 05, 2025 am 12:09 AM

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

What are the SEO optimization techniques for Debian Apache2? What are the SEO optimization techniques for Debian Apache2? May 28, 2025 pm 05:03 PM

DebianApache2's SEO optimization skills cover multiple levels. Here are some key methods: Keyword research: Use tools (such as keyword magic tools) to mine the core and auxiliary keywords of the page. High-quality content creation: produce valuable and original content, and the content needs to be conducted in-depth research to ensure smooth language and clear format. Content layout and structure optimization: Use titles and subtitles to guide reading. Write concise and clear paragraphs and sentences. Use the list to display key information. Combining multimedia such as pictures and videos to enhance expression. The blank design improves the readability of text. Technical level SEO improvement: robots.txt file: Specifies the access rights of search engine crawlers. Accelerate web page loading: optimized with the help of caching mechanism and Apache configuration

See all articles