How to access multiple projects with one domain name in Nginx
Matching introduction of location module
1.”=" prefix directive matching, if the match is successful, other matching will be stopped.
2. Ordinary string instructions are matched in order from longest to shortest. If the successfully matched location uses ^~, other matching will be stopped (regular matching).
3. Match the regular expression instructions in the order in the configuration file. If successful, other matching will be stopped.
4. If there is a successful match in the third step, use the result, otherwise use the second step result.
Notes
1. The matching order is to match ordinary strings first, and then match regular expressions. In addition, the matching order of ordinary strings is based on the character length in the configuration from long to short, which means that the order of locations configured using ordinary strings is irrelevant. In the end, nginx will match according to the length of the configuration, but it should be noted that Regular expressions are tested in the order specified in the configuration file. Finding the first matching regular expression will stop the search.
2. Under normal circumstances, regular expression location matching will be performed after the ordinary string location is successfully matched. There are two ways to change this behavior. One is to use the "=" prefix. At this time, strict matching is performed, and other matching is stopped immediately after the match is successful, and the request is processed at the same time. The other is to use the "^~" prefix. , if used with a regular string, tells nginx not to test the regular expression if the path matches.
location = /uri
= starts with an exact match, and it will only take effect if it matches exactly.
location ^~ /uri
^~ Prefix matching is performed on the url path at the beginning, and before the regular expression.
location ~ pattern
~ indicates case-sensitive regular matching.
location ~* pattern
~* indicates case-insensitive regular matching.
location /uri
Without any modifier, it also means prefix matching, but after regular matching.
location /
Universal matching, any request that does not match other locations will be matched, which is equivalent to default in switch.
Configuration example
server { listen 80; server_name test.com; index index.html index.htm index.php; charset koi8-r; access_log /var/log/nginx/host.access.log main; # 域名+项目1名称 location ^~ /a1/ { alias /usr/share/nginx/html/a1/public/; } # 域名+项目2名称 location ^~ /a2/ { alias /usr/share/nginx/html/a2/public/; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html/500.html; } #pass the php scripts to fastcgi server listening on 127.0.0.1:9000 location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename /scripts$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
Effect preview
1. Visit a1 project
2. Visit a2 project
The above is the detailed content of How to access multiple projects with one domain name in Nginx. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to fix Nginx 403 Forbidden error? Check file or directory permissions; 2. Check .htaccess file; 3. Check Nginx configuration file; 4. Restart Nginx. Other possible causes include firewall rules, SELinux settings, or application issues.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.
