Nginx保护流量

p1iqtdky  于 2023-08-03  发布在  Nginx
关注(0)|答案(1)|浏览(96)

我正在尝试设置一个Nginx来保护Docker服务的外部流量。我的意图是创建一个代理,使用SSL证书(letsencryptit)监听外部IP,然后将请求路由到docker容器中的不同服务器。到目前为止,我已经成功地保护了外部Nginx服务器。我测试了它可以访问静态文件,SSL工作正常。
问题是,我试图将路径路由到托管Angular应用程序的不安全的nginx服务器,它有一些麻烦。
1.如果我获取以slash [/]结尾的URL,就可以了。
1.如果我获取的URL没有斜杠我得到这个错误

curl: (35) OpenSSL/3.0.8: error:0A00010B:SSL routines::wrong version number

字符串

  1. Angular routing会扰乱此配置。Web应用程序具有从一个视图到其他视图的链接,单击这些链接将内部端口和外部DNS名称混合在一起

我的外部SSL安全的Nginx服务器配置如下:

# web.conf
upstream project1 {
  server nginx:3000;
}

server {
  listen 443 default_server ssl http2;
  listen [::]:443 ssl http2;

  ssl_certificate /etc/letsencrypt/live/my.domain.some/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/my.domain.some/privkey.pem;

  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

  # Extra config
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; always";
  add_header X-Frame-Options SAMEORIGIN;
  add_header X-Content-Type-Options nosniff;
  add_header X-XSS-Protection "1; mode=block";

  server_name my.domain.some;

  location / {
    root /var/www/nginx;
  }

  location /test {
    proxy_pass http://project1;
    proxy_http_version 1.1;
    proxy_set_header    Upgrade         $http_upgrade;
    proxy_set_header    Host            $host;
    proxy_set_header    Connection      'upgrade';
    proxy_cache_bypass  $http_upgrade;
    # Extra config
    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;
    proxy_set_header    X-Forwarded-Proto   https;
    proxy_set_header    X-Forwarded-Host    $host;
    proxy_set_header    X-Forwarded-Port    $server_port;
  }

}


Angular项目托管在Nginx的另一个示例上,其配置为:

server {
  listen 3000;
  listen [::]:3000;

  root /var/www/nginx;

  server_tokens off;

  location /test {
    alias /var/www/nginx;
    autoindex off;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
}


我尝试过直接或使用upstream来传递proxy_pass,但这并没有什么区别URL上的curl得到预期的结果only如果它以斜杠结尾
我期望curl -v -L https://my.domain.some/test(没有最后的斜杠)检索Angular主页,但它返回:

*   Trying 111.111.111.111:443...
* Connected to my.domain.some (111.111.111.111) port 443 (#0)
* ALPN: offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Request CERT (13):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Certificate (11):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN: server accepted h2
* Server certificate:
*  subject: CN=my.domain.some
*  start date: May  5 13:49:51 2023 GMT
*  expire date: Aug  3 13:49:50 2023 GMT
*  subjectAltName: host "my.domain.some" matched cert's "my.domain.some"
*  issuer: C=US; O=Let's Encrypt; CN=R3
*  SSL certificate verify ok.
* using HTTP/2
* h2h3 [:method: GET]
* h2h3 [:path: /test]
* h2h3 [:scheme: https]
* h2h3 [:authority: my.domain.some]
* h2h3 [user-agent: curl/7.88.1]
* h2h3 [accept: */*]
* Using Stream ID: 1 (easy handle 0x55a78c81e680)
> GET /test HTTP/2
> Host: my.domain.some
> user-agent: curl/7.88.1
> accept: */*
> 
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
< HTTP/2 301 
< server: nginx/1.15.12
< date: Mon, 03 Jul 2023 15:42:05 GMT
< content-type: text/html
< content-length: 162
< location: http://my.domain.some:3000/test/
< strict-transport-security: max-age=31536000; includeSubDomains; always
< x-frame-options: SAMEORIGIN
< x-content-type-options: nosniff
< x-xss-protection: 1; mode=block
< 
* Ignoring the response-body
* Connection #0 to host my.domain.some left intact
* Clear auth, redirects to port from 443 to 3000
* Issue another request to this URL: 'http://my.domain.some:3000/test/'
* Switched from HTTP to HTTPS due to HSTS => https://my.domain.some:3000/test/
*   Trying 111.111.111.111:3000...
* Connected to my.domain.some (111.111.111.111) port 3000 (#1)
* ALPN: offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
*  CAfile: /etc/ssl/certs/ca-certificates.crt
*  CApath: /etc/ssl/certs
* OpenSSL/3.0.8: error:0A00010B:SSL routines::wrong version number
* Closing connection 1
curl: (35) OpenSSL/3.0.8: error:0A00010B:SSL routines::wrong version number


我已经搜索了这个错误代码,到目前为止没有解决方案。
我感谢你的努力和时间。先谢谢你了

aelbi1ox

aelbi1ox1#

我在网上浏览了很多帖子有一段时间了,现在我想到了一个解决办法。只需在位置路径后面添加一个斜杠,就会触发nginx的rewrite模块,因此它会按照我的预期处理它,
我在web.conf上修改了这个路径,然后看到了nginx

server {
...

location /test/ { ## <<< Notice this path ends on slash!!
proxy_pass http://project1;
proxy_http_version 1.1;
...
}
}

字符串

相关问题