search
HomeOperation and MaintenanceNginxHow to implement Nginx dynamic and static separation configuration

1. Overview

1.1 The difference between dynamic pages and static pages

  • Static resources: When the user accesses this resource multiple times, the source code of the resource will never change H.

  • Dynamic resources: When a user accesses this resource multiple times, the source code of the resource may send changes.

1.2 What is dynamic and static separation

  • Dynamic and static separation allows dynamic web pages in dynamic websites to separate constant resources and frequently changing resources according to certain rules. After the dynamic and static resources are split, we can cache the static resources according to their characteristics. This is the core idea of ​​website static processing

  • A simple summary of dynamic and static separation is: the separation of dynamic files and static files.

  • Pseudo-static: If the website wants to be searched by search engines, dynamic page static technology freemarker and other template engine technologies

1.3 Why use it Separation of dynamic and static

  • In our software development, some requests require background processing (such as: .jsp, .do, etc.), and some requests do not require background processing ( Such as: css, html, jpg, js, etc. files), these files that do not need to be processed in the background are called static files, otherwise they are dynamic files. Therefore our background processing ignores static files. Some people will say that if I ignore the static files in the background, it will be over. Of course this is possible, but the number of background requests will significantly increase. When we have requirements for resource response speed, we should use this dynamic and static separation strategy to solve the problem.

  • Separation of static and dynamic resources deploys website static resources (HTML, JavaScript, CSS, img and other files) separately from background applications to increase the speed of user access to static code and reduce access to background applications. Here we put the static resources into nginx and forward the dynamic resources to the tomcat server.

  • Therefore, to forward dynamic resources to the tomcat server, we use the reverse proxy mentioned earlier.

2. Nginx realizes dynamic and static separation

2.1 Architecture analysis

How to implement Nginx dynamic and static separation configuration

2.2 Configuration

The principle of dynamic and static separation is very simple. Just match the request URL through location. Create /static/imgs under /Users/Hao/Desktop/Test (any directory) and configure it as follows :

###静态资源访问
server {
  listen       80;
  server_name  static.jb51.com;
  location /static/imgs {
       root /Users/Hao/Desktop/Test;
       index  index.html index.htm;
   }
}
###动态资源访问
 server {
  listen       80;
  server_name  www.jb51.com;
    
  location / {
    proxy_pass http://127.0.0.1:8080;
     index  index.html index.htm;
   }
}

Another kind of resource configuration by access

server {
        listen 80;
        server_name  jb51.net;
        access_log  /data/nginx/logs/jb51.net-access.log main;
        error_log  /data/nginx/logs/jb51.net-error.log;
 
        #动态访问请求转给tomcat应用处理
        location ~ .(jsp|page|do)?$ {      #以这些文件结尾的
           proxy_set_header  Host $host;
           proxy_set_header  X-Real-IP  $remote_addr;
           proxy_pass http://tomcat地址;
        }
 
        #设定访问静态文件直接读取不经过tomcat
        location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$  {     #以这些文件结尾的
           expires      30d;
           root /data/web/html ;
        }
}

3. The difference between dynamic and static separation and front and rear separation:

  • dynamic and static separation of dynamic resources and static resources Separate and will not be deployed on the same server.

  • Separation of front and back: website architecture model, microservice development is based on SOA and is oriented to server development, both the backend and the front end adopt the calling interface method. Split a project into a controlWeb (front-end) and interface (back-end), and finally use rpc remote calling technology. The view layer and business logic layer are split, and RPCremote calling technology is used in the middle

4. Some questions

  • Why In Internet company projects, a timestamp will be added after the static resource url? His role: control caching

    • Purpose: The ultimate purpose is to When the control project goes online, the static resources are cached by old browsers to avoid conflicts.

    • Solution: Add timestamp specification t = project online

  • ##304 Principle of local cache status code:

    • The default browser image cache is 7 days.

    • When downloading resources for the first time, the client saves the modified resource time

    • When downloading resources for the second time, the server determines whether the client Whether the last modified time on the client needs to return 200 or 304

    • When downloading the resource for the second time, the server determines whether the current resource file and the client’s last modified time need to return 200 Still 304 The client downloaded the resource for the second time and the last modification time was 2018/6/28 11:07:11 pm

    • The last modification time of the server was greater than the last modification time of the client 200 Reload resources

    • If the last modified time on the server is less than the last modified time on the client, return 304 and go to local cache

Production There may be a

conflict between the last modification time of js css in the environment and the last modification time of the client cache. The server went online on May 22, 2018, and the user accessed it on June 1, 2018. The last modification time of the user's new js file online on June 5, 2018 still retains the last online time. So generally speaking, when the server goes online, a timestamp will be added to the static resources to force the latest resources

The above is the detailed content of How to implement Nginx dynamic and static separation configuration. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache: Understanding the Key DifferencesNGINX and Apache: Understanding the Key DifferencesApr 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.

NGINX Unit: Key Features and CapabilitiesNGINX Unit: Key Features and CapabilitiesApr 25, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports multiple programming languages ​​and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function