网站建设求职,合肥企业建网站,直接玩的网页游戏,商丘免费网站建设开发公司我们可以详细地配置 Nginx 来实现正向代理、反向代理、SSL、负载均衡和虚拟域名。同时#xff0c;我会介绍如何使用一些中间件来监控 Nginx 的状态和性能。
1. 安装 Nginx
如果你还没有安装 Nginx#xff0c;可以通过以下命令进行安装#xff08;以 Ubuntu 为例#xff0…我们可以详细地配置 Nginx 来实现正向代理、反向代理、SSL、负载均衡和虚拟域名。同时我会介绍如何使用一些中间件来监控 Nginx 的状态和性能。
1. 安装 Nginx
如果你还没有安装 Nginx可以通过以下命令进行安装以 Ubuntu 为例
bash
sudo apt update
sudo apt install nginx
2. 配置 Nginx
2.1 正向代理
正向代理通常用于客户端通过代理服务器访问互联网上的资源。Nginx 默认不支持正向代理需要手动启用。
2.1.1 编译 Nginx 并启用 ngx_http_proxy_module
确保 ngx_http_proxy_module 已启用。默认情况下它应该已经包含在标准的 Nginx 包中。
如果需要重新编译并启用更多模块可以参考以下步骤
bash
wget http://nginx.org/download/nginx-1.21.3.tar.gz
tar -xzvf nginx-1.21.3.tar.gz
cd nginx-1.21.3/
./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream --with-maildynamic --with-mail_ssl_module --with-http_sub_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_slice_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_image_filter_moduledynamic --with-http_geoip_moduledynamic --with-http_perl_moduledynamic --with-google_perftools_module --with-debug
make make install
2.1.2 配置正向代理
编辑 Nginx 配置文件 /etc/nginx/nginx.conf 或者创建一个新的配置文件在 /etc/nginx/conf.d/ 目录下例如 forward_proxy.conf。
stream {upstream forward_proxy {server 0.0.0.0:8080;}server {listen 8080;proxy_pass forward_proxy;proxy_bind $remote_addr;proxy_timeout 60s;proxy_connect_timeout 5s;proxy_next_upstream on;proxy_next_upstream_timeout 5s;proxy_next_upstream_tries 3;}
}
在这个配置中
stream 块定义了一个流上下文。upstream forward_proxy 块定义了一个名为 forward_proxy 的服务器组。server 块监听 8080 端口并将所有请求代理到 forward_proxy 上游服务器组。
2.2 反向代理
反向代理通常用于客户端通过代理服务器访问后端服务器。
2.2.1 配置反向代理
编辑 Nginx 配置文件 /etc/nginx/nginx.conf 或者创建一个新的配置文件在 /etc/nginx/conf.d/ 目录下例如 reverse_proxy.conf。
http {upstream backend {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server {listen 80;location / {proxy_pass http://backend;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}}
}
在这个配置中
upstream backend 块定义了一个名为 backend 的服务器组。server 块监听 80 端口并将所有请求代理到 backend 上游服务器组。proxy_set_header 指令用于设置转发请求时的一些头信息。
2.3 SSL
为 Nginx 配置 SSL 证书以加密通信。
2.3.1 获取 SSL 证书
你可以从 Lets Encrypt 获取免费的 SSL 证书或者使用自签名证书。
bash
sudo apt-get install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
2.3.2 配置 SSL
编辑 Nginx 配置文件 /etc/nginx/sites-available/default 或者创建一个新的配置文件在 /etc/nginx/conf.d/ 目录下例如 ssl.conf。
server {listen 80;server_name yourdomain.com www.yourdomain.com;return 301 https://$host$request_uri;
}server {listen 443 ssl;server_name yourdomain.com www.yourdomain.com;ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;ssl_prefer_server_ciphers on;location / {proxy_pass http://backend;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}
}
在这个配置中
第一个 server 块将 HTTP 请求重定向到 HTTPS。第二个 server 块监听 443 端口并配置了 SSL 证书。location 块将所有请求代理到 backend 上游服务器组。
2.4 负载均衡
负载均衡已经在前面的反向代理配置中实现。这里再次强调一下。
2.4.1 配置负载均衡
编辑 Nginx 配置文件 /etc/nginx/conf.d/reverse_proxy.conf。
http {upstream backend {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server {listen 80;location / {proxy_pass http://backend;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}}
}
在这个配置中
upstream backend 块定义了一个名为 backend 的服务器组。server 块监听 80 端口并将所有请求代理到 backend 上游服务器组。proxy_set_header 指令用于设置转发请求时的一些头信息。
2.5 虚拟域名
配置多个虚拟域名以托管不同的网站或服务。
2.5.1 配置虚拟域名
编辑 Nginx 配置文件 /etc/nginx/sites-available/site1 和 /etc/nginx/sites-available/site2然后创建符号链接到 sites-enabled 目录。
server {listen 80;server_name site1.example.com;location / {root /var/www/site1;index index.html index.htm;}
}
server {listen 80;server_name site2.example.com;location / {root /var/www/site2;index index.html index.htm;}
}
创建符号链接
bash
sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-enabled/
重启 Nginx 以应用更改
bash
sudo systemctl restart nginx
3. 监控 Nginx 的中间件
3.1 Nginx Status Module
Nginx 自带的 ngx_http_stub_status_module 模块可以提供基本的状态信息。
3.1.1 启用 Status 模块
确保 ngx_http_stub_status_module 已启用。默认情况下它应该已经包含在标准的 Nginx 包中。
3.1.2 配置 Status 模块
编辑 Nginx 配置文件 /etc/nginx/conf.d/status.conf。
server {listen 80;server_name localhost;location /nginx_status {stub_status on;allow 127.0.0.1; # 允许本地访问deny all; # 拒绝其他所有访问}
}
3.1.3 查看 Status 页面
通过浏览器访问 http://your_nginx_server/nginx_status 可以看到类似以下的信息
Active connections: 2
server accepts handled requests12 12 18
Reading: 0 Writing: 1 Waiting: 1
3.2 Prometheus Grafana
Prometheus 是一个开源的监控和警报工具包而 Grafana 是一个强大的数据可视化平台。结合使用这两个工具可以实现对 Nginx 的全面监控。
3.2.1 安装 Prometheus Exporter
你可以使用 nginx-vts-module 或 nginx-lua-prometheus 等模块来收集 Nginx 的指标数据。
使用 nginx-vts-module
首先需要重新编译 Nginx 并加入 nginx-vts-module。
bash
git clone https://github.com/vozlt/nginx-module-vts.git
wget http://nginx.org/download/nginx-1.21.3.tar.gz
tar -xzvf nginx-1.21.3.tar.gz
cd nginx-1.21.3/
./configure --add-module../nginx-module-vts
make make install
然后在 Nginx 配置文件中添加 VTS 模块的相关配置
http {vhost_traffic_status_zone;upstream backend {server backend1.example.com;server backend2.example.com;server backend3.example.com;}server {listen 80;location / {proxy_pass http://backend;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}location /status {vhost_traffic_status_display;vhost_traffic_status_display_format json;allow 127.0.0.1;deny all;}}
} }
使用 nginx-lua-prometheus
首先需要安装 OpenResty 和 Lua 库。
bash
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:openresty/ppa
sudo apt-get update
sudo apt-get install openresty liblua5.1-cjson-dev
然后在 Nginx 配置文件中添加 Lua 模块的相关配置
http {lua_package_path /path/to/lua-resty-core/lib/?.lua;;;init_by_lua_block {prometheus require(prometheus).init(prometheus_metrics)metric_requests prometheus:counter(nginx_http_requests_total, Number of HTTP requests, {method, host, status})metric_connections prometheus:gauge(nginx_http_connections, Number of HTTP connections, {state})}log_by_lua_block {metric_requests:inc(1, {ngx.var.request_method, ngx.var.server_name, ngx.var.status})metric_connections:set(ngx.var.connections_active, {active})metric_connections:set(ngx.var.connections_reading, {reading})metric_connections:set(ngx.var.connections_writing, {writing})metric_connections:set(ngx.var.connections_waiting, {waiting})}server {listen 80;location /metrics {content_by_lua_block {prometheus:collect()}}location / {proxy_pass http://backend;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;}}
}
3.2.2 安装 Prometheus
下载并安装 Prometheus
bash
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
tar xvfz prometheus-2.30.3.linux-amd64.tar.gz
cd prometheus-2.30.3.linux-amd64/ 编辑 prometheus.yml 文件以抓取 Nginx 的指标数据
Yaml
global:scrape_interval: 15sscrape_configs:- job_name: nginxstatic_configs:- targets: [localhost:9113] # 根据实际情况调整目标地址
启动 Prometheus
bash
./prometheus --config.fileprometheus.yml
3.2.3 安装 Grafana
下载并安装 Grafana
bash
sudo apt-get install -y software-properties-common wget
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo deb https://packages.grafana.com/oss/deb stable main | sudo tee -a /etc/apt/sources.list.d/grafana.list
sudo apt-get update
sudo apt-get install grafana
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
打开浏览器访问 http://your_grafana_server:3000登录 Grafana默认用户名和密码为 admin/admin然后添加 Prometheus 数据源。
最后导入 Nginx 的仪表盘模板可以从 Grafana 社区找到合适的模板 ID。
4. 总结
通过上述步骤我们成功配置了 Nginx 实现正向代理、反向代理、SSL、负载均衡和虚拟域名并介绍了几种常用的监控中间件来监控 Nginx 的状态和性能。以下是整个架构图 这个架构展示了前端如何通过 Nginx 进行正向代理、反向代理、SSL 加密、负载均衡和虚拟域名管理以及如何使用 Prometheus 和 Grafana 来监控 Nginx 的状态和性能。
希望这些信息对你有所帮助