


nginx reverse proxy, separation of dynamic and static requests, and nginx cache application, and use ngx_cache_purge to clear the specified URL
一,nginx反向代理配置
#tomcat
Java代码
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- erver{
- listen 80;
- server_name www.codes51.com;
- location / {
- proxy_redirect off;
- 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://tomcat_server;
- }
显然就是用户访问www.codes51.com(需要设置本地localhost,将www.codes51.com指向nginx所在IP)的时候(或将www.codes51.com直接写在nginx所在的IP地址),将请求转到到后台的tomcat服务器,即127.0.0.1:8080,并将请求到的数据转发给client
二,动静态请求相分离
神马意思?图片,JS,HTML等静态的东西去访问一台专门的服务器,而动态的请求去访问另一台服务器。就这么简单,上例子:
Java代码
- server {
- listen 192.168.154.128:80;
- server_name image.codes51.com;
- index index.html;
- #proxy_pass http://tomcat_server;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root html;
- #index index.html index.htm;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
- {
- valid_referers none blocked 192.168.154.128 192.168.154.1;
- if ($invalid_referer)
- {
- rewrite ^ /403.jpg break;
- }
- if (!-f $request_filename) {
- rewrite ^ /404.jpg last;
- }
- expires 30d;
- }
- #error_page 404 /404.html;
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /404.jpg {
- root html;
- }
- }
- tomcat
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- server{
- listen 192.168.154.128;
- server_name www.codes51.com;
- location / {
- proxy_redirect off;
- 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://tomcat_server;
- }
- }
上面这种方式是通过设定不同的域名,可不可以在同一个域名中,通过判断后缀来将动态与静态请求相分离呢?
Java代码
- #tomcat
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- server{
- listen 192.168.154.128;
- server_name www.codes51.com;
- location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
- {
- root html;
- }
- location ~ .*.(jsp|do)$ {
- proxy_redirect off;
- 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://tomcat_server;
- }
OK!同一个域名,根据后缀不同,请求不同的服务,实现动态静态请求相分离。仔细想一想,如果又出现了一种静态的文件,比如*.abc ,那难道又去修改那个配制文件吗?显然不太合理,所以可以考虑将所有的表态文件放在同一个根目录下面,比如/static那么可以将上面的静态页面请求修改一下:
Java代码
- location /static
- {
- root html/static;
- }
咦这样是不是就好一些了,而且文件的存放也比较有规范了。
三,nginx缓存应用
nginx具有web缓存服务,proxy_cache,但是有一个问题就是,proxy_cache不能清除指定的URL缓存,只能设置URL过期时间,但是有问题,有人就会很快解决问题,nginx第三方模块ngx_cache_purge能清除指定URL。
nginx安装时需要将ngx_cache_purege加载进去。
Java代码
- ./configure --user=www --group=www --add-module=/root/dxm/nginx/ngx_cache_purge-1.2
其中,/root/dxm/nginx/ngx_cache_purge-1.2为ngx_cache_purge解压路径(附件中提供ngx_cache_purge tar包下载)
现在来一段实例,实现图片缓存:
By the way, proxy_tem_path and proxy_cache_path must be under the same partition!
Java code
- proxy_temp_path /usr/local/nginx/proxy_temp;
- proxy_cache_path /usr/local/nginx/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;
Java code
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- server{
- listen 192.168.154.128;
- server_name www.codes51.com;
- location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
- {
- proxy_cache cache_one;
- proxy_cache_methods GET HEAD POST;
- proxy_cache_min_uses 1;
- proxy_cache_valid 200 302 10m;
- proxy_cache_valid 404 1m;
- proxy_cache_valid any 1m;
- proxy_cache_key "$host:$server_port$uri$is_args$args";
- proxy_redirect off;
- 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://tomcat_server;
- }
- location ~ .*.(jsp)$ {
- proxy_redirect off;
- 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://tomcat_server;
- }
- location ~ /purge(/.*)
- Allow 192.168.154.128;
- Allow 192.168.154.1;
- deny all; proxy_cache_purge cache_one $host:$server_port$
- 1$is_args$args; }
-
Well, static pages are cached, but dynamic requests are not cached!
Take a look at the purege configuration in the last paragraph. Obviously, it indicates which IPs can manually clear the specified URL.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor