Nginx 是一个高性能的 HTTP 和反向代理服务器,也提供了 IMAP/POP3/SMTP 代理服务。本文将为您介绍 Nginx 的基本使用方法和配置。
安装 Nginx
在 Linux 系统中,您可以使用以下命令安装 Nginx:
sudo apt-get update
sudo apt-get install nginx
在 Windows 系统中,您可以从 Nginx 官网 下载安装程序。
配置 Nginx
Nginx 的配置文件位于 /etc/nginx/nginx.conf
(Linux)或 nginx.conf
(Windows)。
以下是一个简单的 Nginx 配置示例:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 默认服务器
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# 其他配置...
}
}
Nginx 的基本功能
- 静态文件服务:Nginx 可以作为静态文件服务器,例如 HTML、CSS、JavaScript 和图片文件。
- 反向代理:Nginx 可以将请求转发到后端服务器,实现负载均衡和请求分发。
- 负载均衡:Nginx 支持多种负载均衡算法,如轮询、最少连接数等。
- 缓存:Nginx 可以缓存静态文件,减少对后端服务器的请求。
扩展阅读
Nginx Logo