nginx location
时间: 2023-09-04 20:15:56 浏览: 66
The location directive in Nginx allows you to define how Nginx should handle requests for specific URLs or URI paths. It is used to map requested URLs to the location of the files or scripts that handle them.
The location directive is typically used within a server block in the Nginx configuration file. Here's an example of how it can be used:
```
server {
listen 80;
server_name example.com;
location / {
# Handle requests for the root URL
# Serve the index.html file located in the root directory
root /var/www/example.com/;
index index.html;
}
location /api {
# Handle requests for URLs starting with /api
# Proxy the requests to a backend server running on port 8080
proxy_pass http://localhost:8080;
}
location ~ \.php$ {
# Handle requests for URLs ending with .php
# Pass the requests to a PHP FastCGI server running on port 9000
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
In this example, the first location block handles requests for the root URL and serves the index.html file located in the /var/www/example.com/ directory. The second location block handles requests for URLs starting with /api and proxies them to a backend server running on port 8080. The third location block uses a regular expression to match URLs ending with .php and passes them to a PHP FastCGI server running on port 9000.
You can use the location directive to define different behaviors for different URLs or URI paths, such as serving static files, proxying requests to a backend server, or processing requests with a specific script or application.
阅读全文