# Nginx可视化配置-NginxConfig
> 项目地址:https://github.com/digitalocean/nginxconfig.io 项目体验地址:https://do.co/nginxconfig
## Nginx简介
*Nginx* (engine x) 是一个高性能的[HTTP](https://baike.baidu.com/item/HTTP)和[反向代理](https://baike.baidu.com/item/反向代理/7793488)web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为[俄罗斯](https://baike.baidu.com/item/俄罗斯/125568)访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。
`Nginx` 是开源、高性能、高可靠的 `Web` 和反向代理服务器,而且支持热部署,几乎可以做到 7 * 24 小时不间断运行,即使运行几个月也不需要重新启动,还能在不间断服务的情况下对软件版本进行热更新。性能是 `Nginx` 最重要的考量,其占用内存少、并发能力强、能支持高达 5w 个并发连接数,最重要的是, `Nginx` 是免费的并可以商业化,配置使用也比较简单。
### Nginx安装
CentOS:
```
yum install nginx -y
```
Ubuntu:
```
sudo apt update
sudo apt install nginx
```
### Node安装
CentOS:
```
yum install nodejs
```
Ubuntu:
```
sudo apt install nodejs
sudo apt install npm
```
## 引言
`Nginx`作为Web服务器,功能强大,好用。每次使用`vim`修改conf配置文件时,很容易经常出错,令人炸毛。想必各位初学Nginx进行配置时也会遇到这种情况。那么现在,有一款可视化Nginx配置工具--`NginxConfig`,可视化页面操作,支持中文,实时导出。简直是Nginx配置的神器。

看一看它的效果图。例如,实现反向代理,将含有`/hosp/`路径的请求代理到`http://127.0.0.1:8201`,将含有`/cmn/`的路径请求代理到`http:127.0.0.1:8202`

再来看自动生成的配置文件。

看一看`nginxconfig.io-127.0.0.1.tar.gz`里都包含了什么?

将下载好的配置文件(解压)上传服务器的`/etc/nginx`目录,然后重启Nginx。搞定!相比手动使用`vim`配置好用极了。
最后,我们再看看`NginxConfig`可以配置那些选项?



站点配置的选项有
- 服务
- HTTPS
- PHP
- Python
- 反向代理
- 路由设置
- 日志记录
- 限制
- 洋葱
全局配置的选项则有
- HTTPS
- 安全
- Python
- 反向代理
- 性能
- 日志记录
- NGINX
- Docker
- 工具
不妨来简单实战一下,来看看我的`halo`博客的Nginx是如何配置的?又如何用`NginxConfig`进行配置呢?
```conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
upstream halo {
server 127.0.0.1:8090;
}
server {
listen 80;
listen [::]:80;
server_name www.zhoujk.top;
client_max_body_size 1024m;
location / {
proxy_pass http://halo;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
#前缀为www.你的域名配置文件及ssl证书
#SSL 访问端口号为 443
listen 443 ssl;
#填写绑定证书的域名
server_name www.zhoujk.top;
##证书文件名称
ssl_certificate /usr/local/nginx/cert/6279393_zhoujk.top.pem;
##私钥文件名称
ssl_certificate_key /usr/local/nginx/cert/6279393_zhoujk.top.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM- SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://halo;
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#gzip on;
# server {
# listen 80;
# server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# root html;
# index index.html index.htm;
# }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root html;
# }
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
#}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
```
好!`NginxConfig`就简单为此!快去动手尝试一下吧!
项目源码地址:[https://github.com/digitalocean/nginxconfig.io](https://github.com/digitalocean/nginxconfig.io)
项目体验地址:[https://do.co/nginxconfig](https://do.co/nginxconfig)
# 附录1
## Nginx常用命令
`systemctl` 系统命令:
```
# 开机配置
systemctl enable nginx # 开机自动启动
systemctl disable nginx # 关闭开机自动启动
# 启动 Nginx
systemctl start nginx # 启动Nginx成功后,可以直接访问主机IP,此时会展示Nginx默认页面
# 停止 Nginx
systemctl stop nginx
# 重启 Nginx
systemctl restart nginx
# 重新加载 Nginx
systemctl reload nginx
# 查看 Nginx 运行状态
systemctl status nginx
# 查看 Nginx 进程
ps -ef | grep nginx
# 杀死 Nginx 进程
kill -9 pid # 根据上面查看到的 Nginx 进程号,杀死 Nginx 进程,-9 表示强制结束进程
```
`Nginx` 应用程序命令:
```
nginx -s reload # 向主进程发送信号,重新加载配置文件,热重启
nginx -s reopen # 重启 Nginx
nginx -s stop # 快速关闭
nginx -s quit # 等待工作进程处理完成后关闭
nginx -T # 查看当前 Nginx 最终的配置
nginx -t # 检查配置是否有问题
```
## Nginx 核心配置
#### 配置文件结构
`Nginx` 的典型配置示例:
```
# main段配置信息
user nginx; # 运行用户,默认即是 Nginx,可以不进行设置
worker_processes auto; # Nginx 进程数,一般设置为和 CPU 核数一样
error_log /var/log/nginx/error.log warn; # Nginx 的错误日志存放目录
pid /var/run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
# events段配置信息
events {
use epoll; # 使用 epoll 的 I/O 模型(如果你不知道 Nginx 该使用哪种轮询方法,会自动选择一个最适合你操作系统的)
worker_connections 1024; # 每个进程允许最大并发数
}
# http 段配置信息
# 配置使用最频繁的部分,代理、缓存、日志定义等绝大多数功能和第三方模块的配置都在这里设置
http {
# 设置日志模式
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; # Nginx 访问日志存放位置
sendfile on; # 开启高效传输模式
tcp_nopush on; # 减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; # 保持连接的时间,也叫超时时间,单位秒
types_hash_max_size 2048;
include /etc/nginx/mime.types; # 文件扩展名与类型映射表
default_type application/octet-stream; # 默认文件类型
include /etc/nginx/conf.d/*.conf; # 加载子配置项
# server段配置信息
server {
listen 80; # 配置监听的端口
server_name localhost; # 配置的域名
# location段配置信息
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认首页文件
deny 172.168.22.11; # 禁止访问的 ip 地址,可以为 all
allow 172.168.33.44;# 允许访问的 ip 地址,可以为 all
}
error_page 500 502 503 504 /50x.html; # 默认 50x 对应的访问页面
error_page 400 404 error.html; # 同上
}
}
```
- `main` 全局配置,对全局生效;
- `events` 配置影响 `Nginx` 服务器与用户的网络连接;
- `http` 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置;
- `server` 配置虚拟主机的相关参数,一个 `http` 块中可以有多个 `server` 块;
- `location` 用于配置匹配的 `uri` ;
- `upstream` 配置后端服务器具体地址,负载均衡配置不可或缺的部分。
# 附录2
尝试本地构建开发:
1. Create the nginxconfig directory
```
mkdir nginxconfig
cd nginxconfig
```
2. Clone the repository
```
git clone https://github.com/digitalocean/nginxconfig.io
```
3. Install NPM packages
```
npm ci
或者
npm install
```
4. Run the development server *(with file watchers)*
```
npm run dev
```
5. Open the development site `localhost:8080`
```
DONE Compiled successfully in 4766ms 10:58:44
99% done plugins webpack-bundle-analyzerWebpack Bundle Analyzer saved report to D:\NginxConfig\nginxconfig.io\dist\report.html
99% done plugins vue-cli-service serve
App running at:
- Local: http://localhost:8080
- Network: unavailable
Note that the development build is not optimized.
To create a production build, run npm run build.
```
# 附录3

项目地址:https://github.com/russelltao/geektime-nginx
推荐阅读:《Nginx核心知识100讲》 陶辉

Nginx可视化配置-NginxConfig