search
HomeOperation and MaintenanceNginxHow to configure nginx multiple locations

How to configure nginx multiple locations

May 17, 2023 pm 10:25 PM
nginxlocation

Preface

Configuring multiple locations under the nginx server performs different processing according to different path matches.

nginx commonly used regular expressions

Grammar rules: location [=|~|~*|^~] /uri/ { … }

## The beginning of
  • #= 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 measurement

server {
    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;	
    }  
}

Remarks

The 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.txt

location ^~ /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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
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.

Using NGINX Unit: Deploying and Managing ApplicationsUsing NGINX Unit: Deploying and Managing ApplicationsApr 22, 2025 am 12:06 AM

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 vs. Apache: A Comparative Analysis of Web ServersNGINX vs. Apache: A Comparative Analysis of Web ServersApr 21, 2025 am 12:08 AM

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.

NGINX Unit's Advantages: Flexibility and PerformanceNGINX Unit's Advantages: Flexibility and PerformanceApr 20, 2025 am 12:07 AM

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 vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

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.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

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 in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

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

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools