Nginx:無法解析的主機名稱 (HTTPS)

Nginx:無法解析的主機名稱 (HTTPS)

我正在嘗試為 API 設定最小的 nginx 配置,但無法讓 SSL/HTTPS 工作。

長話短說:/healthcheck端點按預期工作,但沒有其他任何效果。


我有一個 API 應用程式在 GCE 實例上的 Docker 容器中與 nginx 一起運行。

           Internet
               +
               |
               |
               v
+--------------+----------------+
|             GCP               |
|                               |
|    +--------------------+     |
|    |Google Load Balancer|     |
|    +---------+----------+     |
|              |                |
|              |                |
|              |                |
|              v                |
|   +----------+------------+   |
|   | Google Compute Engine |   |
|   |                       |   |
|   | +-------------------+ |   |
|   | |  Server instance  | |   |
|   | |                   | |   |
|   | | +------+  +-----+ | |   |
|   | | |Docker|  |nginx| | |   |
|   | | |      |  +-----+ | |   |
|   | | | API  |          | |   |
|   | | +------+          | |   |
|   | +-------------------+ |   |
|   +-----------------------+   |
|  ---------------------------  |
+-------------------------------+

所有 HTTP 流量都會經過負載平衡器並到達 nginx。如果端點是,/healthcheck它會直接轉到 API(返回200 OK),而其他所有內容都應透過負載平衡器作為 HTTPS 路由回來。

所有 HTTPS 流量都應直接進入 API。

server我的配置中有兩個區塊。
第一個區塊:

server {
    listen 80;
    server_name  my-domain.com;

    location /healthcheck {
        proxy_pass http://API_IP:8080/healthcheck;
    }

    location / {
        return 301 https://$server_name$request_uri;
    }
}

第二塊:

server {
    listen 443 ssl;
    server_name my-domain.com;

    location / {
        proxy_pass http://API_IP:8080$request_uri;
    }
}

隨著失眠 REST 用戶端
當我點擊/healthcheckHTTP 或 HTTPS 時,它可以工作,但/users/reviews只給出錯誤訊息Error: Couldn't resolve host name。沒有狀態代碼,只有此錯誤訊息。


任何和所有的幫助將不勝感激。


更新

輸出來自wget -S my-domain.com/users
(實際網域名稱和IP已更改)

$ wget -S my-domain.com/users
--2018-08-30 14:09:08--  http://my-domain.com/users
Resolving my-domain.com (my-domain.com)... *IP REDACTED*
Connecting to my-domain.com (my-domain.com)|*IP REDACTED*|:80... connected.
HTTP request sent, awaiting response...
  HTTP/1.1 301 Moved Permanently
  Server: nginx/1.10.3 (Ubuntu)
  Date: Thu, 30 Aug 2018 12:09:09 GMT
  Content-Type: text/html
  Content-Length: 194
  Location: https://my-domain.com/users
  Via: 1.1 google
Location: https://my-domain.com/users [following]
--2018-08-30 14:09:09--  https://my-domain.com/users
Resolving my-domain.com (my-domain.com)... failed: Name or service not known.
wget: unable to resolve host address ‘dev.api.godlypatruljen.no’

答案1

正確的。

我從一位同事那裡得到了一些建議,這讓我走上了正軌。它可能不是最漂亮的解決方案,但它確實有效。

我將兩個舊server塊替換為新的:

server {
    listen 80;
    listen [::]:80;
    server_name my-domain.com;

    set $redirect_to_https 0;

    if ($http_x_forwarded_proto != 'https') {
        set $redirect_to_https 1;
    }

    if ($request_uri = '/healthcheck') {
        set $redirect_to_https 0;
    }

    if ($redirect_to_https = 1) {
        return 301 https://$host$request_uri;
    }

    location / {
        proxy_pass http://$api_ip:$api_port;
    }
}

我還有一些額外的locations 和ifs (除了此處顯示的內容之外),因為我需要處理一些邊緣情況(伺服器上的靜態位置,但不在容器內部等),但這個範例應該說明解決方案相當好。

相關內容