Last update at :2024-02-07,Edit by888u
Reverse proxy
To understand simply, a reverse proxy is actually a springboard. When you access A, A will access B and return the content of B to you. For a more rigorous definition, please read the information from other websites copied later in my article. . . .
Why use a reverse proxy
There are several situations where you may need a reverse proxy:
- For example, if you rent a server in France and the speed in mainland China is too slow, you can find a vps in Hong Kong to reverse the French server. In this way, when you access this website, the transit speed through Hong Kong will change. quick. And because the anti-generation server does not require performance, the price is very cheap.
- If you don’t want to expose the real IP address, you can build a reverse proxy server on the front end so that users can access the reverse proxy server.
nginx anti-generation tutorial
The simplest reverse proxy code. The location of the code is the website configuration file of nginx. It depends on the panel you installed. For example: the lnmp panel is placed in "/usr/local/apache/conf/vhost/domain name. conf", Pagoda can be modified directly in the panel, and a previous article introduced how to reverse generation of Pagoda. Today we mainly introduce nginx. If you installed and compiled nginx manually, you can also do it.
server { listen 80; server_name kxceping.com; location/{ proxy_pass http://cache.kxceping.com/; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }The meaning of this code is that when you visit kxceping.com, the content you actually get is the content of cache.kxceping.com. The purpose of reverse proxying cache.kxceping.com through kxceping.com was successfully achieved.
But this is a direct connection. If we can temporarily cache the requested content on the reverse generation server, the access speed will be faster. For example, if you use Hong Kong to replace France's machine, if the request is cached on the Hong Kong server once, then subsequent people will access the file directly from the Hong Kong server, which is much faster.
Reverse proxy code for front-end caching
First we need to create a directory to store the cache:
mkdir /home/cache/path -p mkdir /home/cache/temp -pThen add cache settings in the nginx configuration file httpd.conf Please insert the following code into http{ ## here }. Generally, it can be added above or below log_format:
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; proxy_temp_path /home/cache/temp; proxy_cache_path /home/cache/path levels=1:2 keys_zone=cache_one:100m inactive=7d max_size=5g;#100m is the memory occupied, 7d is deleted without access for 7 days, and 5g is the hard disk space occupied by the cache
Finally, add cache settings to the website’s configuration file
server { listen 80; server_name kxceping.com; location/{ proxy_cache cache_one; proxy_cache_valid 200 304 3d; proxy_cache_key $host$uri$is_args$args; proxy_pass http://cache.kxceping.com/; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; expires 10d; access_log /home/wwwlogs/vpsmm.log access; } }Instructions for several configurations: server_name kxceping.com # Host name proxy_cache_valid 200 304 3d; # Normal cache time 3 days proxy_pass http://cache.kxceping.com/; # Anti-generation website expires 10d; #Default 10 days cache access_log /home/wwwlogs/fandai.log access; # Log file
Of course, if you are familiar with the configuration of nginx, you can also cache only images, js or only a certain directory according to the above code.
It should be noted that once this cache is set, it will not be automatically deleted until it expires. For example, if you cache the homepage for 3 days and you update an article, it will not be automatically updated to the cache server. Users will always access the content from 3 days ago. To solve this problem. The cache time can be set short, such as 1h (1 hour). Or directly manually clear the contents of the two cache folders set above. As for the method of clearing the corresponding cache when using the program to automatically update, it is a high-end application. I will teach you later if I have time.
Recommended site search: free website domain name application, vps server rental, European server, Yunnan server rental domain name registration official website, domain name registration website, IP query, high-defense server rental, vps virtual host station group server,
发表评论