NC 使用Nginx实现https的反向代理

打印 上一主题 下一主题

主题 531|帖子 531|积分 1593

summary: [通过Nginx实现NCC的https访问,并解决UClient应用的问题]

1 概述

通过Nginx 安装配置反向代理,实现NC、NCC的https访问。
本文以NCC2005为例,说明如何配置Nginx、NCC,以及解决UClient无法访问https代理的问题。
其他版本的NC可以以此类推。
2 实现需求

NCC2005应用地址:http://10.16.51.147:2005
Nginx_1.22.0服务地址:10.16.51.147
最终实现代理访问:
https://10.16.51.147 -> http://10.16.51.147:2005
https://10.16.51.147:4433 ->  http://10.16.51.147:2005
http://10.16.51.147 -> http://10.16.51.147:2005
3 解决方案

3.1 Nginx配置

nginx.conf文件配置如下:
  1. # Generated by nginxconfig.io
  2. # See nginxconfig.txt for the configuration share link
  3. user                 root;
  4. pid                  /run/nginx.pid;
  5. worker_processes     auto;
  6. worker_rlimit_nofile 65535;
  7. #error_log  logs/error.log;
  8. #error_log  logs/error.log  notice;
  9. #error_log  logs/error.log  info;
  10. # Load modules
  11. include              /usr/local/nginx/conf/modules-enabled/*.conf;
  12. events {
  13.     multi_accept       on;
  14.     worker_connections 65535;
  15. }
  16. http {
  17.     charset                utf-8;
  18.     sendfile               on;
  19.     tcp_nopush             on;
  20.     tcp_nodelay            on;
  21.     server_tokens          off;
  22.     log_not_found          off;
  23.     types_hash_max_size    2048;
  24.     types_hash_bucket_size 64;
  25.     client_max_body_size   2048M;
  26.     # MIME
  27.     include                mime.types;
  28.     default_type           application/octet-stream;
  29.     # Logging
  30.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  31.     #                  '$status $body_bytes_sent "$http_referer" '
  32.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  33.     access_log             logs/access.log;
  34.     error_log              logs/error.log warn;
  35.     # SSL
  36.     ssl_session_timeout    1d;
  37.     ssl_session_cache      shared:SSL:10m;
  38.     ssl_session_tickets    off;
  39.     # Diffie-Hellman parameter for DHE ciphersuites
  40.     # ssl_dhparam            /usr/local/nginx/conf/dhparam.pem;
  41.     # Mozilla Old configuration
  42.     ssl_protocols             SSLv3 TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  43.     ssl_ciphers               ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA;
  44.     ssl_prefer_server_ciphers on;
  45.     # OCSP Stapling
  46.     # ssl_stapling           on;
  47.     # ssl_stapling_verify    on;
  48.     # resolver               1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
  49.     # resolver_timeout       2s;
  50.     # ssl_trusted_certificate /usr/local/nginx/conf/ssl/ncc2005.cxjk.com.crt;
  51.     # Connection header for WebSocket reverse proxy
  52.     # map $http_upgrade $connection_upgrade {
  53.     #     default upgrade;
  54.     #     ""      close;
  55.     # }
  56.     # map $remote_addr $proxy_forwarded_elem {
  57.     #     # IPv4 addresses can be sent as-is
  58.     #     ~^[0-9.]+$        "for=$remote_addr";
  59.     #     # IPv6 addresses need to be bracketed and quoted
  60.     #     ~^[0-9A-Fa-f:.]+$ "for="[$remote_addr]"";
  61.     #     # Unix domain socket names cannot be represented in RFC 7239 syntax
  62.     #     default           "for=unknown";
  63.     # }
  64.     # map $http_forwarded $proxy_add_forwarded {
  65.     #     # If the incoming Forwarded header is syntactically valid, append to it
  66.     #     "~^(,[ \\t]*)*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?)*([ \\t]*,([ \\t]*([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?(;([!#$%&'*+.^_`|~0-9A-Za-z-]+=([!#$%&'*+.^_`|~0-9A-Za-z-]+|"([\\t \\x21\\x23-\\x5B\\x5D-\\x7E\\x80-\\xFF]|\\\\[\\t \\x21-\\x7E\\x80-\\xFF])*"))?)*)?)*$" "$http_forwarded, $proxy_forwarded_elem";
  67.     #     # Otherwise, replace it
  68.     #     default "$proxy_forwarded_elem";
  69.     # }
  70.     # Load configs
  71.     include /usr/local/nginx/conf/conf.d/*.conf;
  72.     # ncc2005.cxjk.com
  73.     server {
  74.         listen                             443 ssl;
  75.         listen                             [::]:443 ssl;
  76.         server_name                        10.16.51.147;
  77.         # SSL
  78.         ssl_certificate                    /usr/local/nginx/ssl/server.crt;
  79.         ssl_certificate_key                /usr/local/nginx/ssl/server.key;
  80.         
  81.         # security headers
  82.         # add_header X-XSS-Protection        "1; mode=block" always;
  83.         # add_header X-Content-Type-Options  "nosniff" always;
  84.         # add_header Referrer-Policy         "no-referrer-when-downgrade" always;
  85.         # add_header Content-Security-Policy "default-src 'self' http: https: ws: wss: data: blob: 'unsafe-inline'; frame-ancestors 'self';" always;
  86.         # add_header Permissions-Policy      "interest-cohort=()" always;
  87.         
  88.         # deny access to .htaccess files, if Apache's document root
  89.         # concurs with nginx's one
  90.         #
  91.         # . files
  92.         location ~ /\.(?!well-known) {
  93.             deny all;
  94.         }
  95.         # logging
  96.         access_log logs/ncc2005.cxjk.com.access.log;
  97.         error_log  logs/ncc2005.cxjk.com.error.log warn;
  98.         # reverse proxy
  99.         location / {
  100.             proxy_pass                         http://10.16.51.147:2005;
  101.             proxy_set_header Host              $http_host; # ncc必须设置
  102.             proxy_http_version                 1.1; # 定义用于代理的HTTP协议版本,默认情况下将其设置为1.0。对于Websocket和 keepalive 连接,需要使用1.1版
  103.             
  104.             # proxy_cache_bypass                 $http_upgrade; # 设置不从缓存中获取响应的条件
  105.             # Proxy SSL
  106.             proxy_ssl_server_name              on;
  107.             # Proxy headers
  108.             # 如果应用程序使用Websockets,则这些Upgrade、Connection字段是必填字段
  109.             # proxy_set_header Upgrade           $http_upgrade;
  110.             # proxy_set_header Connection        $connection_upgrade;
  111.             proxy_set_header Connection                   "";
  112.             proxy_set_header X-Real-IP         $remote_addr; # 将真实的访客远程IP地址转发到代理服务器
  113.             proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for; # ncc必须设置(包含客户端被代理的每个服务器的IP地址的列表)
  114.             # proxy_set_header X-Forwarded-Proto $scheme; # ncc不可设置此参数(在HTTPS服务器块中使用时,来自代理服务器的每个HTTP响应都会重写为HTTPS)
  115.             proxy_set_header X-Forwarded-Host  $host; # 定义客户端请求的原始主机
  116.             proxy_set_header X-Forwarded-Port  $server_port; # 定义客户端请求的原始端口
  117.             # Proxy timeouts 超时设置不宜过短,否则类似导入数据这样的长业务场景会发生upstream time out错误
  118.             proxy_connect_timeout              7200s;
  119.             proxy_send_timeout                 7200s;
  120.             proxy_read_timeout                 7200s;
  121.             proxy_redirect http:// $scheme://; # ncc必须设置,否则轻量端访问异常(将后端响应header location内容中的http://替换成用户端协议https://)。如果做了http强转https,且都是采用默认端口(80,443),则可不设置此参数,因为重定向后的http:80会转为https:443。
  122.             proxy_headers_hash_max_size 512;
  123.             proxy_headers_hash_bucket_size 128;
  124.         }
  125.         # favicon.ico
  126.         location = /favicon.ico {
  127.             log_not_found off;
  128.             access_log    off;
  129.         }
  130.         # robots.txt
  131.         location = /robots.txt {
  132.             log_not_found off;
  133.             access_log    off;
  134.         }
  135.         #error_page  404              /404.html;
  136.         # redirect server error pages to the static page /50x.html
  137.         #
  138.         error_page   500 502 503 504  /50x.html;
  139.         location = /50x.html {
  140.             root   html;
  141.         }
  142.         # gzip
  143.         gzip            on;
  144.         gzip_vary       on;
  145.         gzip_proxied    any;
  146.         gzip_comp_level 6;
  147.         gzip_types      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
  148.         gzip_min_length 10k;
  149.         gzip_disable "MSIE [1-6]\.";
  150.     }
  151.     # 4433 端口
  152.     server {
  153.         listen                             4433 ssl;
  154.         listen                             [::]:4433 ssl;
  155.         server_name                        10.16.51.147;
  156.         # SSL
  157.         ssl_certificate                    /usr/local/nginx/ssl/server.crt;
  158.         ssl_certificate_key                /usr/local/nginx/ssl/server.key;
  159.             
  160.         # deny access to .htaccess files, if Apache's document root
  161.         # concurs with nginx's one
  162.         #
  163.         # . files
  164.         location ~ /\.(?!well-known) {
  165.             deny all;
  166.         }
  167.         # logging
  168.         access_log logs/ncc2005.cxjk.com.access.log;
  169.         error_log  logs/ncc2005.cxjk.com.error.log warn;
  170.         # reverse proxy
  171.         location / {
  172.             proxy_pass                         http://10.16.51.147:2005;
  173.             proxy_set_header Host              $http_host; # ncc必须设置
  174.             proxy_http_version                 1.1; # 定义用于代理的HTTP协议版本,默认情况下将其设置为1.0。对于Websocket和 keepalive 连接,需要使用1.1版
  175.             # Proxy SSL
  176.             proxy_ssl_server_name              on;
  177.             # Proxy headers
  178.             proxy_set_header X-Real-IP         $remote_addr; # 将真实的访客远程IP地址转发到代理服务器
  179.             proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for; # ncc必须设置(包含客户端被代理的每个服务器的IP地址的列表)
  180.             proxy_set_header X-Forwarded-Host  $host; # 定义客户端请求的原始主机
  181.             proxy_set_header X-Forwarded-Port  $server_port; # 定义客户端请求的原始端口
  182.             # Proxy timeouts 超时设置不宜过短,否则类似导入数据这样的长业务场景会发生upstream time out错误
  183.             proxy_connect_timeout              7200s;
  184.             proxy_send_timeout                 7200s;
  185.             proxy_read_timeout                 7200s;
  186.             proxy_redirect http:// $scheme://; # ncc必须设置,否则轻量端访问异常(将后端响应header location内容中的http://替换成用户端协议https://)
  187.             proxy_headers_hash_max_size 512;
  188.             proxy_headers_hash_bucket_size 128;
  189.         }
  190.         # favicon.ico
  191.         location = /favicon.ico {
  192.             log_not_found off;
  193.             access_log    off;
  194.         }
  195.         # robots.txt
  196.         location = /robots.txt {
  197.             log_not_found off;
  198.             access_log    off;
  199.         }
  200.         #error_page  404              /404.html;
  201.         # redirect server error pages to the static page /50x.html
  202.         #
  203.         error_page   500 502 503 504  /50x.html;
  204.         location = /50x.html {
  205.             root   html;
  206.         }
  207.         # gzip
  208.         gzip            on;
  209.         gzip_vary       on;
  210.         gzip_proxied    any;
  211.         gzip_comp_level 6;
  212.         gzip_types      text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
  213.         gzip_min_length 10k;
  214.         gzip_disable "MSIE [1-6]\.";
  215.     }
  216.     # HTTP redirect http转为https
  217.     server {
  218.         listen      80;
  219.         listen      [::]:80;
  220.         server_name 10.16.51.147;
  221.         # 两种方式均可
  222.         return      301 https://10.16.51.147$request_uri;
  223.         # rewrite ^(.*) https://10.16.51.147$1 permanent;
  224.     }
  225. }
复制代码
3.2 NCC配置

完成Nginx配置后,轻量端就可以访问了。如果还需要使用UClient访问轻量端及重量端,则需要执行以下步骤:

  • 安装补丁《patch_ncc2005_20221017_Uclient无法正确下载https应用问题_v1.1_liyas》


  • 配置文件
    配置https应用,在以下文件中加入参数设置:scheme=https (该参数不为空时,优先使用该参数设置的协议,否则使用UClient传入的protocol)
轻量端https协议配置:ncchome\hotwebs\nccloud\WEB-INF\config\uclient_appconfig.properties
重量端https协议配置:ncchome\webapps\nc_web\Client\uclient_appconfig.properties
4 常见问题

4.1 浏览器无法访问应用

https://10.16.51.147/nccloud可以正常访问,而非443端口https://10.16.51.147:4433/nccloud无法访问,需要在Nginx中加入配置proxy_redirect http:// $scheme://;
4.2 UClient无法正常下载和访问轻量端及重量端的ncc应用

因为当前版本的UClient在搜索下载应用时,会将https协议变更为http,导致无法正确下载应用。该问题需要通过上述的补丁解决。
相关问题具体表现如下:
4.2.1 下载应用错误


  • 搜索https://10.16.51.147,下载的重量端应用为http://host:80,点击无法方法应用。
  • 搜索https://10.16.51.147,下载的轻量端应用为http://host:80/nccloud,点击无法访问
  • 搜索https://10.16.51.147:4433,无法下载重量端,无法下载轻量端
4.2.2 80端口通过Nginx强转后无法在Uclient访问

通过https://10.16.51.147 下载的轻量端应用实际访问地址是http://host:80/nccloud,Nginx已经做了强转,但是点击应用后提示页面错误,无法访问URLhttps://10.16.51.147/nccloud,
再次点击该URL,则可正常进入系统。
该问题需要UClient修复。
4.2.3 手工修复本地app.esc的应用下载地址,安装应用后访问异常

手工修改轻量端应用的app.esc下载地址为https://10.16.51.147:4433/nccloud,安装轻量应用,点击应用访问地址栏访问的是http://10.16.51.147:4433/nccloud,页面提示400错误:The plain HTTP request was sent to HTTPS por。
问题现象及方案同问题1。
5 延伸阅读

NC-UClient下载安装应用详解

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

老婆出轨

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表