我是 NGINX 的新手,它确实让我头疼......
我有一台运行着多项服务的服务器:confluent/kafka、grafana。这些服务中的每一个都可以通过 http://:port 从域中的其他计算机访问。
我想使用 HTTPS 反向代理这些服务以提高安全性,以便 confluent 和 grafana 只能通过 URI 访问
https://<hostname>/grafana
https://<hostname>/confluent
https://<hostname>/more to come
好的,也会
https://grafana.<hostname>
https://confluent.<hostname>
https://more_to_come.<hostname>
- 项目清单
到目前为止我有一个 nginx.conf(或多或少是默认模板):
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
... # mail all commented out
如果没有其他文件(服务器配置),我在.works中创建了两个*.conf
文件:conf.d
grafana.conf
*.conf
server {
listen 80;
listen [::]:80;
listen 443 ssl;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
root /var/www/<hostname>/html;
index index.html index.htm;
server_name <hostname>;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Server $host;
}
}
注意: 处有一个虚拟网页/var/www/<hostname>/html/index.html
。但是,如果我只有一个,grafana.conf
则不会将其拉出,而是拉出 URI 处的 Grafana 应用程序https://<hostname>
(或多或少符合预期)。
如果我添加另一个.conf
文件,请confluent.conf
像这样:
server {
listen 80;
listen [::]:80;
listen 443 ssl;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
root /var/www/<hostname>/confluent/html;
index index.html index.htm;
server_name <hostname>;
location /confluent {
proxy_pass http://localhost:9091;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Server $host;
}
}
系统返回:
http://<hostname>
-->/var/www/<hostname>/html/index.html
https://<hostname>
-->/var/www/<hostname>/html/index.html
https://<hostname>/confluent
-->502 Bad Gateway
答案1
我会坚持使用单服务器配置,使用映射来定义每个主机名的根和池。如下所示:
map $http_host $docroot {
grafana.statnamen.de /var/www/www.statnamen.de/html;
confluent.statnamen.de /var/www/www.statnamen.de/confluent/html;
moretocome.statnamen.de /var/www/www.statnamen.de/moretocome/html;
default /var/www/html;
}
map $http_host $pool {
grafana.statnamen.de localhost:3000;
confluent.statnamen.de localhost:9091;
moretocome.statnamen.de 127.0.0.1:9092;
default 127.0.0.1:3000;
}
server {
listen 80;
listen [::]:80;
listen 443 ssl;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
root /var/www/$docroot;
index index.html index.htm;
server_name grafana.statnamen.de confluent.statnamen.de moretocome.statnamen.de;
location / {
proxy_pass http://$pool;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Server $host;
}
}