nginx 入门配置说明
upstream if-all-aiops {
server localhost:12001;
}
server {
listen 8999 ;
server_name localhost;
root D:/softinstall/develop/aiops-nginx-1.24.0/html;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;
client_max_body_size 500M;
charset utf-8;
access_log logs/aiops.access.log main;
location / {
return 302 /aiops/;
}
location /login {
alias D:/softinstall/develop/aiops-nginx-1.24.0/html/login;
index index.html index.htm;
autoindex on;
error_page 404 =200 /login/index.html;
}
location /aiops {
alias D:/softinstall/develop/aiops-nginx-1.24.0/html/aiops;
index index.html index.htm;
autoindex on;
error_page 404 /aiops/index.html;
}
location ~ ^/aiops/if/(.*)$ {
proxy_pass http://if-all-aiops/if/$1?$args;
proxy_cookie_path /aiops/ /;
add_header Cache-Control "no-cache, no-store";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect ~^http://localhost/if/(.*) https://localhost/aiops/if/$1;
}
location ~ ^/aiops/logout(.*)$ {
proxy_pass http://if-all-aiops/logout$1?$args;
proxy_cookie_path /aiops/ /;
add_header Cache-Control "no-cache, no-store";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/if/(.*)$ {
proxy_pass http://if-all-aiops/if/$1?$args;
proxy_cookie_path /aiops/ /;
add_header Cache-Control "no-cache, no-store";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
说明:
这是一个 Nginx 配置文件的示例,下面对每部分进行详细解释。
### 1. `upstream`
```nginx
upstream if-all-aiops {
server localhost:12001;
}
```
这是一个定义了负载均衡的 upstream 块。这里的 `if-all-aiops` 将请求转发到 `localhost` 上的 `12001` 端口。`upstream` 可以用于多个服务器实例的负载均衡,但这里只定义了一个。
### 2. `server` 块
```nginx
server {
listen 9991;
server_name localhost;
root D:/softinstall/develop/aiops-nginx-1.24.0/html;
...
}
```
- **`listen 9991;`**:Nginx 将在 `9991` 端口监听传入的请求。
- **`server_name localhost;`**:这是服务器的名称,通常包含请求的域名。
- **`root`**:定义了默认的文件根目录,即静态文件的存放位置。
### 3. SSL 配置
```nginx
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;
```
这里配置了 SSL 相关的设置,主要包括:
- **`ssl_session_cache`**:共享 SSL 会话缓存,设置为 `1m`。
- **`ssl_session_timeout`**:定义 SSL 会话的超时间隔,这里设置为 `10分钟`。
- **`ssl_prefer_server_ciphers on;`**:服务器优先使用其配置的密码套件。
### 4. 客户端配置
```nginx
client_max_body_size 500M;
charset utf-8;
access_log logs/aiops.access.log main;
```
- **`client_max_body_size 500M;`**:设置客户端请求体的最大大小,这里为 `500MB`。
- **`charset utf-8;`**:指定字符集为 UTF-8。
- **`access_log`**:定义访问日志的存储路径和日志格式。
### 5. 位置匹配 (`location`)
#### 5.1. 默认重定向
```nginx
location / {
return 302 /aiops/;
}
```
所有请求到根路径 `/` 将被重定向到 `/aiops/`,并返回一个 302 状态码(临时重定向)。
#### 5.2. 登录页面
```nginx
location /login {
alias D:/softinstall/develop/aiops-nginx-1.24.0/html/login;
index index.html index.htm;
autoindex on;
error_page 404 =200 /login/index.html;
}
```
- **`alias`**:定义了一个别名指向登录页面的目录。
- **`index`**:设置默认的索引文件。
- **`autoindex on;`**:允许列出目录中的文件。
- **`error_page 404 =200 /login/index.html;`**:当访问 `/login` 的资源未找到时,返回 `index.html`,而不是 `404` 错误。
#### 5.3. AIops 页面
```nginx
location /aiops {
alias D:/softinstall/develop/aiops-nginx-1.24.0/html/aiops;
index index.html index.htm;
autoindex on;
error_page 404 /aiops/index.html;
}
```
与登录页面的配置类似,为 `/aiops` 路径设置了静态文件目录、索引文件和目录列表。
#### 5.4. 代理请求
接下来的几个 `location` 块设置了请求的代理转发。
- **/aiops/if/**:
```nginx
location ~ ^/aiops/if/(.*)$ {
proxy_pass http://if-all-aiops/if/$1?$args;
...
}
```
匹配所有以 `/aiops/if/` 开头的请求,并将其代理到 `http://localhost:12001/if/`。URL 参数也会被添加。
- **/aiops/logout**:
```nginx
location ~ ^/aiops/logout(.*)$ {
proxy_pass http://if-all-aiops/logout$1?$args;
...
}
```
将以 `/aiops/logout` 开头的请求代理到 `http://localhost:12001/logout`。
- **/if/**:
```nginx
location ~ ^/if/(.*)$ {
proxy_pass http://if-all-aiops/if/$1?$args;
...
}
```
这个配置与上面 `/aiops/if/` 的配置类似,但处理的是 `/if/` 的请求。
### 6. 请求头设置与缓存控制
在多个 `location` 块中都可以看到这些配置:
```nginx
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
```
这些行用于设置在代理请求中传递给后端服务器的 HTTP 头信息,包括请求的主机、真实 IP 地址等。
这些配置帮助确保后端服务能够正确了解客户端发来的请求来源。
### 总结
这个 Nginx 配置文件实现了一个相对复杂的反向代理,主要用于转发请求到后端服务和提供静态文件服务。它还配置了 SSL、安全控制和错误处理,使得整个 Web 服务更加稳定和可靠。如果你对某一特定部分有更深入的问题,请随时问我!
更多推荐


所有评论(0)