【问题标题】:Catch specific server on nginx在 nginx 上捕获特定服务器
【发布时间】:2017-09-01 18:19:52
【问题描述】:

我的 nginx 文件如下所示:

server {
    listen 443 ssl;
    server_name local.awesome.com;

    ssl_certificate /opt/certs/local.awesome.com.crt;
    ssl_certificate_key /opt/certs/local.awesome.com.key;

    location / {
        root /var/www/awesome.com/public_html/;
        index index.html;
    }
}
server {
    listen 443 ssl;
    server_name api.local.awesome.com;

    ssl_certificate /opt/certs/local.awesome.com.crt;
    ssl_certificate_key /opt/certs/local.awesome.com.key;

    root /var/www/api.awesome.com/public_html/;

    # Known locations for static resources
    location /resources/ {
    }

    # Process all other requests via JS in index.html
    location / {
        rewrite .* /index.html;
        break;
    }

    location /api {
        rewrite "^/api/(.*)$" /$1 break;
        proxy_pass http://api:8001;
    }
}

如果我查询类似于:

GET https://api.local.awesome.com/api/

这很好用。

我决定让它在全球范围内访问以共享一些数据。

我正在尝试请求:

GET https://192.168.1.3:443/api/

但这不起作用。它返回HTTP/1.1 404 Not Found

此请求返回403 Forbidden

GET https://192.168.1.3:443/

看起来这里的一切都是经过授权的,但我希望之前的请求应该返回与Not Found不同的东西。

这里出了什么问题以及如何替换:

GET https://api.local.awesome.com/api/

GET http://192.168.1.3:443/api/

如果架构或端口不同,这对我来说并不重要。

有什么建议吗?

更新:

curl -v http://192.168.1.3/api/
*   Trying 192.168.1.3...
* TCP_NODELAY set
* Connected to 192.168.1.3 (192.168.1.3) port 80 (#0)
> GET /api/ HTTP/1.1
> Host: 192.168.1.3
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.13.3
< Date: Fri, 01 Sep 2017 18:55:05 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: https://192.168.1.3/api/
< 
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.13.3</center>
</body>
</html>
* Connection #0 to host 192.168.1.3 left intact

【问题讨论】:

  • 您希望两者同时工作还是只工作一个?
  • 只有一个工作别名就足够了。

标签: http nginx https proxy http-proxy


【解决方案1】:

下面改

listen 443 ssl;
server_name api.local.awesome.com;

listen 443 ssl;
listen 80;
server_name api.local.awesome.com _;

listen 443 ssl;
listen 80;
server_name api.local.awesome.com 192.168.1.3;

这将允许您使用 http://192.168.1.3/api/ 访问它

【讨论】:

  • 它返回HTTP/1.1 301 Moved Permanently。我已更改为您的第二个建议。
  • 这是您发布的完整配置,还是您还有更多内容?还有curl -v http://192.168.1.3/api/的输出是什么@
  • 我已经用 curl 的响应更新了这个问题。这是来自配置文件的所有配置,但看起来 80 端口可能被 nginx 容器使用:docker ps | grep nginx 2258ae964073 nginx:alpine "nginx -g 'daemon ..." 8 hours ago Up 8 minutes 0.0.0.0:80-&gt;80/tcp, 0.0.0.0:443-&gt;443/tcp
  • 您还有一些配置在容器内执行 http 到 https 重定向。您发布的配置不是 nginx 的唯一配置。
  • 对不起,你是对的。我发现了一个带有此重定向的附加配置文件:# Permanently redirect all HTTP requests via HTTPS to the same URI. server { listen 80 default_server; return 301 https://$host$request_uri; }
猜你喜欢
  • 1970-01-01
  • 2016-04-01
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
  • 2015-08-19
相关资源
最近更新 更多