以下是 Nginx 配置的详细教程,涵盖从基本安装到常见的配置场景。
—
## 一、安装 Nginx
### 1. **在 Linux 系统上安装**
– **Debian/Ubuntu:**
“`bash
sudo apt update
sudo apt install nginx
“`
– **CentOS/Red Hat:**
“`bash
sudo yum install epel-release
sudo yum install nginx
“`
– **启动服务**:
“`bash
sudo systemctl start nginx
sudo systemctl enable nginx
“`
– **检查状态**:
“`bash
sudo systemctl status nginx
“`
### 2. **在 Windows 或 macOS 上安装**
– **Windows**: 从 [Nginx 官方站点](https://nginx.org/) 下载二进制文件并解压运行。
– **macOS**: 使用 Homebrew 安装:
“`bash
brew install nginx
“`
启动 Nginx:
“`bash
nginx
“`
—
## 二、Nginx 配置文件结构
Nginx 的默认配置文件路径一般是:
– `/etc/nginx/nginx.conf`(Linux 系统)
– `/usr/local/etc/nginx/nginx.conf`(macOS)
以下是 Nginx 配置的详细教程,涵盖从基本安装到常见的配置场景。
—
## 一、安装 Nginx
### 1. **在 Linux 系统上安装**
– **Debian/Ubuntu:**
“`bash
sudo apt update
sudo apt install nginx
“`
– **CentOS/Red Hat:**
“`bash
sudo yum install epel-release
sudo yum install nginx
“`
– **启动服务**:
“`bash
sudo systemctl start nginx
sudo systemctl enable nginx
“`
– **检查状态**:
“`bash
sudo systemctl status nginx
“`
### 2. **在 Windows 或 macOS 上安装**
– **Windows**: 从 [Nginx 官方站点](https://nginx.org/) 下载二进制文件并解压运行。
– **macOS**: 使用 Homebrew 安装:
“`bash
brew install nginx
“`
启动 Nginx:
“`bash
nginx
“`
—
## 二、Nginx 配置文件结构
Nginx 的默认配置文件路径一般是:
– `/etc/nginx/nginx.conf`(Linux 系统)
– `/usr/local/etc/nginx/nginx.conf`(macOS)
### 配置文件结构:
“`nginx
worker_processes 1; # 定义工作进程数量
events {
worker_connections 1024; # 每个进程的最大连接数
}
http {
include mime.types; # 文件类型映射
default_type application/octet-stream;
sendfile on; # 开启高效传输模式
keepalive_timeout 65; # 连接超时时间
server {
listen 80; # 监听的端口
server_name localhost;# 服务器名称
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认主页
}
}
}
“`
—
## 三、常见配置场景
### 1. **基本 HTTP 服务**
配置一个简单的网站服务,监听 80 端口:
“`nginx
server {
listen 80;
server_name example.com;
location / {
root /var/www/html; # 网站根目录
index index.html; # 默认文件
}
}
“`
将你的静态网站文件放在 `/var/www/html` 目录下即可。
—
### 2. **启用 HTTPS(SSL)**
如果你有 SSL 证书,可以配置 HTTPS:
“`nginx
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
location / {
root /var/www/html;
index index.html;
}
}
“`
如果没有证书,可以使用 **Let’s Encrypt** 免费获取:
– 安装 Certbot:
“`bash
sudo apt install certbot python3-certbot-nginx
“`
– 自动生成证书并配置 Nginx:
“`bash
sudo certbot —
配置文件结构:
“`nginx
worker_processes 1; # 定义工作进程数量
events {
worker_connections 1024; # 每个进程的最大连接数
}
http {
include mime.types; # 文件类型映射
default_type application/octet-stream;
sendfile on; # 开启高效传输模式
keepalive_timeout 65; # 连接超时时间
server {
listen 80; # 监听的端口
server_name localhost;# 服务器名称
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认主页
}
}
}
“`
—
## 三、常见配置场景
### 1. **基本 HTTP 服务**
配置一个简单的网站服务,监听 80 端口:
“`nginx
server {
listen 80;
server_name example.com;
location / {
root /var/www/html; # 网站根目录
index index.html; # 默认文件
}
}
“`
将你的静态网站文件放在 `/var/www/html` 目录下即可。
—
### 2. **启用 HTTPS(SSL)**
如果你有 SSL 证书,可以配置 HTTPS:
“`nginx
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
location / {
root /var/www/html;
index index.html;
}
}
“`
如果没有证书,可以使用 **Let’s Encrypt** 免费获取:
– 安装 Certbot:
“`bash
sudo apt install certbot python3-certbot-nginx
“`
– 自动生成证书并配置 Nginx:
“`bash
sudo certbot —
暂无评论内容