AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1157549
Accepted
JJandke
JJandke
Asked: 2024-04-08 18:31:31 +0800 CST2024-04-08 18:31:31 +0800 CST 2024-04-08 18:31:31 +0800 CST

nginx prox_pass:端口 3000 被重定向到 80。与其他端口的配置相同

  • 772

我有以下场景:

服务A

服务 A 在 下可用host:8080。

我在 nginx 中配置了反向代理来servicea.domain解析host:8080.

这是我的配置文件(位置:/etc/nginx/sites-available/servicea)


server {
    listen 80;
    listen [::]:80;

    server_name servicea.domain.com;

    location / {
        proxy_pass http://host:8080/admin/;
        include proxy_params;

    proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 90;
        proxy_set_header X-Forwarded-Proto $scheme;

    set $xforwardedssl "off";
    if ($scheme = https) {
            set $xforwardedssl "on";
    }
    }
}

服务B

我想对服务 B (Grafana)做同样的事情。这可以在 下实现host:3000。/etc/nginx/sites-available/serviceb下的我的 nginx-config如下所示:


server {
    listen 80;
    listen [::]:80;

    server_name serviceb.domain.com;

    location / {
        proxy_pass http://host:3000/;
        include proxy_params;

    proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 90;
        proxy_set_header X-Forwarded-Proto $scheme;

    set $xforwardedssl "off";
    if ($scheme = https) {
            set $xforwardedssl "on";
    }
    }
}

  • 这两个文件都符号链接到/etc/nginx/sites-enabled/。
  • Nginx 启动成功并且没有抱怨。
  • 调用时一切正常servicea.domain。
  • 当我打电话时,serviceb.domain我在浏览器中收到 400 错误代码。

当我使用 wget 加载页面时,我发现它实际上并未解析为 host:3000 而是解析为 host:80。


╰─$ wget serviceb.domain.com
Will not apply HSTS. The HSTS database must be a regular and non-world-writable file.
ERROR: could not open HSTS store at '/home/config/.wget-hsts'. HSTS will be disabled.
--2024-04-08 12:17:00--  http://serviceb.domain.com/
Resolving serviceb.domain.com (serviceb.domain.com)... 10.25.25.34
Connecting to serviceb.domain.com (serviceb.domain.com)|10.25.25.34|:80... connected.
HTTP request sent, awaiting response... 400 Bad Request
2024-04-08 12:17:03 ERROR 400: Bad Request.

这是为什么?我有同样的配置1:1?一点证明配置是相同的。这是 diff 的输出:


╰─$ diff serviceb servicea
5c5
<     server_name servicea.domain.com;
---
>     server_name serviceb.domain.com;
8c8
<         proxy_pass http://host:8080/admin/;
---
>         proxy_pass http://host:3000/;

谁能给我一个提示,在哪里可以找到覆盖我的反向代理或以其他方式影响名称解析的设置?如果您需要更多信息,请告诉我。

先感谢您!

nginx
  • 1 1 个回答
  • 24 Views

1 个回答

  • Voted
  1. Best Answer
    JJandke
    2024-04-10T18:00:04+08:002024-04-10T18:00:04+08:00

    没关系。问题已经解决。

    这里需要澄清一下:

    • wget 命令中的端口 80 只是反向代理上请求的端口,它应该是这样的。
    • 配置文件有问题。然而,与此同时,很多事情都发生了变化。

    这是我们当前使用的配置,供通过互联网来到这里的任何人使用。

    服务-a

    
    #########################################################################################
    # Service A --> /etc/nginx/conf.d/service-a.conf
    
    server {
      server_name service-a.domain.tld;
        location / {
            proxy_pass http://host-a:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_read_timeout 90;
            proxy_set_header X-Forwarded-Proto $scheme;
            set $xforwardedssl "off";
            if ($scheme = https) {
                set $xforwardedssl "on";
             }
            proxy_set_header X-Forwarded-Ssl $xforwardedssl;
        }
    
        listen [::]:443 ssl; 
        listen 443 ssl; 
        ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem; 
        include /etc/letsencrypt/options-ssl-nginx.conf; 
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
    
    
    }
    server {
        if ($host = service-a.domain.tld:8080) {
            return 301 https://$host$request_uri;
        } 
    
    
      listen 80;
      listen [::]:80;
      server_name service-a.domain.tld;
        return 404; 
    }
    
    

    服务-b (Grafana)

    
    #########################################################################################
    # Service B (Grafana) /etc/nginx/conf.d/service-b.conf
    
    server {
      server_name service-b.domain.tld;
        location / {
            proxy_pass http://host-a:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_read_timeout 90;
            proxy_set_header X-Forwarded-Proto $scheme;
            set $xforwardedssl "off";
            if ($scheme = https) {
                set $xforwardedssl "on";
             }
            proxy_set_header X-Forwarded-Ssl $xforwardedssl;
        }
    
        listen [::]:443 ssl; 
        listen 443 ssl; 
        ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem; 
        include /etc/letsencrypt/options-ssl-nginx.conf; 
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
    
    
    }
    server {
        if ($host = service-b.domain.tld:3000) {
            return 301 https://$host$request_uri;
        } 
    
    
      listen 80;
      listen [::]:80;
      server_name service-b.domain.tld;
        return 404; 
    }
    
    

    这里是更改的部分/etc/grafana/grafana.ini:

    
    #################################### Server ####################################
    [server]
    # Protocol (http, https, h2, socket)
    protocol = https
    
    # The ip address to bind to, empty will bind to all interfaces
    http_addr =
    
    # The http port  to use
    http_port = 3000
    
    # The public facing domain name used to access grafana from a browser
    domain = domain.tld
    
    # Redirect to correct domain if host header does not match domain
    # Prevents DNS rebinding attacks
    enforce_domain = false
    
    # The full public facing url you use in browser, used for redirects and emails
    # If you use reverse proxy and sub path specify full url (with sub path)
    ;root_url = %(protocol)s://%(domain)s:%(http_port)s/
    root_url = https://subdomain.domain.tld:3000
    
    
    # https certs & key file
    cert_file = /etc/grafana/grafana.crt
    cert_key = /etc/grafana/grafana.key
    
    
    

    有关 Grafana 的更多信息,请参阅原始 doku。

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve