Preface
Configuring multiple locations under the nginx server performs different processing according to different path matches.
nginx commonly used regular expressions
## The beginning ofGrammar rules: location [=|~|~*|^~] /uri/ { … }
- #= means: exact match.
- ^~ The beginning means: it is case-sensitive.
- ~ The beginning means: case-sensitive regular matching.
- ~* starts with: case-insensitive regular matching.
- !~ and !~* respectively represent: case-sensitive non-matching and case-insensitive non-matching regular matching.
- / means: universal matching, any request will be matched.
In the case of multiple location configurations, the matching order is (not verified):
Match = first, then ^~ , followed by regular matching in order in the file, and finally handed over to / universal matching. When a match is successful, the matching is stopped and the request is processed according to the current matching rules.
Actual measurementserver {
listen 80;
listen [::]:80;
server_name location.test.com;
access_log /var/log/nginx/location.host.access.log main;
#*********************注意多个location通常按精确的放前面,模糊大范围的放后面,nginx先找= ******************************
location = /login.html {#精确匹配 /login
root /usr/share/nginx/html/test-equal;#请求/login.html相当于寻找资源/usr/share/nginx/html/test-equal/login.html
}
location ^~ /prefix/ {#区分大小写且以/prefix/开头
root /usr/share/nginx/html/test-prefix;#root代表根目录,请求/prefix/prefix.html相当于寻找资源/usr/share/nginx/html/test-prefix/prefix/prefix.html
}
location ~ \.(png|jpg)$ {#不区分大小写且以.png或.jpg结尾
root /usr/share/nginx/html/test-suffix;#请求/suffix/a.png相当于寻找资源/usr/share/nginx/html/test-suffix/suffix/a.png
}
location ^~ /jd/ {# 区分大小写且以/jd/开头
proxy_pass https://www.jd.com/;#proxy_pass 此处的url以/结尾,则nginx会取掉location部分再转发,例如,请求/jd/电器?name=1 则会转发到https://www.jd.com/电器?name=1
}
location ^~ /s {# /会匹配到所有的
proxy_pass https://www.baidu.com;#proxy_pass 此处的url没有以/结尾,则匹配到的地址全部拼接到代理后的地址,例如,请求/s?name=1 则会转发到https://www.baidu.com/s?name=1
}
location / {# 会返回index.html
root /usr/share/nginx/html;
index index.html;
}
}
RemarksThe difference between root and alias under location: Example: Client request: http://localhost:8080/user/info/a.txt If nginx is configured with root: nginx will look for resources: /home/html/user/info/a.txtlocation ^~ /user {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E--> root /home/html;#此处可以不以/结尾 }If nginx is configured with alias: nginx will look for resources: /home/html/info/a.txt
location ^~ /user {<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E--> alias /home/html/;#此处以/结尾 }
The above is the detailed content of How to configure nginx multiple locations. For more information, please follow other related articles on the PHP Chinese website!

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.

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.

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.


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

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

Hot Article

Hot Tools

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

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools
