🤖 Ai回答: Nginx (pronounced engine-x) is a high-performance web server and reverse proxy server that is widely used for serving static content, load balancing, and handling HTTP requests efficiently、It is known for its speed, scalability, and low resource consumption, making it a popular choice for hosting websites and applications.
Below is an overview of Nginx on Linux, including installation, configuration, and common use cases.
1、Installing Nginx on Linux
The installation process varies slightly depending on your Linux distribution、Below are instructions for some common distributions:
Start and Enable Nginx
After installation, start and enable the Nginx service to ensure it runs on boot:
bash
sudo systemctl start nginx
sudo systemctl enable nginx
Check Nginx Status
To verify that Nginx is running:
bash
sudo systemctl status nginx
You can also test the configuration file syntax:
bash
sudo nginx -t
2、Basic Configuration
Nginx's main configuration file is typically located at `/etc/nginx/nginx.conf`、However, most configurations are stored in individual files under `/etc/nginx/sites-available/` and symlinked to `/etc/nginx/sites-enabled/`.
Default Configuration File
The default configuration file for a website is usually located at:
`/etc/nginx/sites-available/default`
`/etc/nginx/conf.d/default.conf`
Example Configuration
Here’s an example of a basic Nginx configuration for a static website:
nginx
server {
listen 80;
server_name 65.hk www.65.hk;
Reload Nginx
After making changes to the configuration file, reload Nginx to apply them:
bash
sudo systemctl reload nginx
3、Common Use Cases
Serving Static Files
Nginx excels at serving static files like HTML, CSS, JavaScript, and images、Simply point the `root` directive to the directory containing your files.
Reverse Proxy
Nginx can act as a reverse proxy to forward requests to backend servers (e.g., Node.js, Python, or PHP applications)、Example:
nginx
server {
listen 80;
server_name 65.hk;
Load Balancing
Nginx can distribute traffic across multiple backend servers for better performance and reliability:
nginx
upstream backend {
server 192.168.1.101;
server 192.168.1.102;
server 192.168.1.103;
}
server {
listen 80;
server_name 65.hk;
location / {
proxy_pass http://backend;
}
}
SSL/TLS Encryption
To secure your website with HTTPS, you can use Let's Encrypt to obtain a free SSL certificate and configure Nginx:
nginx
server {
listen 443 ssl;
server_name 65.hk;
location / {
root /var/www/65.hk;
index index.html;
}
}
Redirect HTTP to HTTPS:
nginx
server {
listen 80;
server_name 65.hk;
return 301 https://$host$request_uri;
}
4、Monitoring and Logs
Nginx logs are stored in `/var/log/nginx/` by default:
Access Log: `/var/log/nginx/access.log`
Error Log: `/var/log/nginx/error.log`
You can monitor logs in real-time using:
bash
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
5、Advanced Features
Caching
Nginx can cache responses from backend servers to improve performance:
nginx
location / {
proxy_cache my_cache;
proxy_pass http://backend;
}
Rate Limiting
Prevent abuse by limiting the number of requests per second:
nginx
http {
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
6、Troubleshooting
Configuration Errors: Use `sudo nginx -t` to check for syntax errors.
Port Conflicts: Ensure no other service is using port 80 or 443.
Firewall Issues: Allow HTTP/HTTPS traffic using `ufw` or `firewalld`:
bash
sudo ufw allow 'Nginx Full'
7、Conclusion
Nginx is a powerful and versatile tool for web hosting, reverse proxying, load balancing, and more、Its lightweight nature and high performance make it ideal for both small and large-scale deployments、By mastering its configuration and features, you can optimize your web infrastructure for speed, security, and reliability.
If you have specific questions about Nginx or need help with a particular setup, feel free to ask!