Nodejs:如何使用Caddy2反向代理+ TLS证书?

j91ykkif  于 2022-12-03  发布在  Node.js
关注(0)|答案(2)|浏览(498)

this is my first time deploying nodejs from localhost to the live server. I am using aapanel for my live server.
Here is the relevant code in node server.js file:

  1. const hostname = 'localhost';
  2. // const hostname = 'www.thespacebar.io';
  3. // set port, listen for requests
  4. const PORT = process.env.PORT || 8080;
  5. app.listen(PORT, hostname, () => {
  6. console.log(`Server is running on port ${PORT}.`);
  7. });

Here is my pm2 settings:

I am unable to open my nodejs app with GET https://www.thespacebar.io:8080 , but it works for GET http://www.thespacebar.io:8080
GET https://www.thespacebar.io:8080 does not work with error:

  1. This site cant provide a secure connection
  2. ERR_SSL_PROTOCOL_ERROR

Anyone know what I did wrong?

EDIT: I have installed Caddy and setup the Caddyfile in /etc/caddy like this:

  1. # The Caddyfile is an easy way to configure your Caddy web server.
  2. #
  3. # Unless the file starts with a global options block, the first
  4. # uncommented line is always the address of your site.
  5. #
  6. # To use your own domain name (with automatic HTTPS), first make
  7. # sure your domain's A/AAAA DNS records are properly pointed to
  8. # this machine's public IP, then replace ":80" below with your
  9. # domain name.
  10. import ./thespacebar.io
  11. :80 {
  12. # Set this path to your site's directory.
  13. root * /usr/share/caddy
  14. # Enable the static file server.
  15. file_server
  16. # Another common task is to set up a reverse proxy:
  17. # reverse_proxy localhost:8080
  18. # Or serve a PHP site through php-fpm:
  19. # php_fastcgi localhost:9000
  20. }
  21. # Refer to the Caddy docs for more information:
  22. # https://caddyserver.com/docs/caddyfile

and created the adjacent file thespacebar.io :

  1. thespacebar.io {
  2. reverse_proxy localhost:8080
  3. }

but when I visit https://thespacebar.io/ , I end up at index.html instead of the JSON { message: "Welcome to bezkoder application." } and POSThttp://www.thespacebar.io/api/verification/callback with body paramverify_token:abcde is supposed to show the JSON:

  1. {
  2. "message": "Callback called successfully."
  3. }

instead of 404 Not Found

EDIT 2: I have removed the portion:

  1. # :80 {
  2. # Set this path to your site's directory.
  3. # root * /usr/share/caddy
  4. # Enable the static file server.
  5. # file_server
  6. # Another common task is to set up a reverse proxy:
  7. # reverse_proxy localhost:8080
  8. # Or serve a PHP site through php-fpm:
  9. # php_fastcgi localhost:9000
  10. # }
  11. # Refer to the Caddy docs for more information:
  12. # https://caddyserver.com/docs/caddyfile

from etc/caddy/Caddyfile
but when I run caddy run Caddyfile and caddy reload Caddyfile , I am getting this error:

  1. [root@vultrguest caddy]# caddy run Caddyfile
  2. 2022/12/02 08:11:44.132 INFO using adjacent Caddyfile
  3. 2022/12/02 08:11:44.132 WARN Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies {"adapter": "caddyfile", "file": "Caddyfile", "line": 12}
  4. 2022/12/02 08:11:44.133 INFO admin admin endpoint started {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
  5. 2022/12/02 08:11:44.133 INFO http server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
  6. 2022/12/02 08:11:44.133 INFO http enabling automatic HTTP->HTTPS redirects {"server_name": "srv0"}
  7. 2022/12/02 08:11:44.133 INFO tls.cache.maintenance started background certificate maintenance {"cache": "0xc000151030"}
  8. 2022/12/02 08:11:44.133 INFO tls.cache.maintenance stopped background certificate maintenance {"cache": "0xc000151030"}
  9. Error: loading initial config: loading new config: http app module: start: listening on :80: listen tcp :80: bind: address already in use
  10. [root@vultrguest caddy]# caddy reload Caddyfile
  11. 2022/12/02 08:11:49.875 INFO using adjacent Caddyfile
  12. 2022/12/02 08:11:49.876 WARN Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies {"adapter": "caddyfile", "file": "Caddyfile", "line": 12}
  13. Error: sending configuration to instance: performing request: Post "http://localhost:2019/load": dial tcp [::1]:2019: connect: connection refused
  14. [root@vultrguest caddy]#

If I run GEThttp://www.thespacebar.io:8080 I get:

  1. Web server is down Error code 521
  2. Visit cloudflare.com for more information.
  3. 2022-12-02 08:22:13 UTC
  4. You
ig9co6j1

ig9co6j11#

Caddy很容易设置为反向代理,它可以轻松地为您加密SSL证书

  1. {
  2. email example@email.com
  3. }
  4. thespacebar.io {
  5. reverse_proxy localhost:8080
  6. }

我看到你发布了一个更新-我想删除的一件事是

  1. :80 {
  2. .....
  3. }

如果你读了你发布的文本,它确实说用你的域替换:80(但不要添加:80或球童不会为该域做证书)
我还看到您没有设置一个带有电子邮件地址的全局部分--我相当肯定,为了让letsencrypt工作,需要有这个部分(不要引用我的话)--至少在我第一次开始使用Caddy时是这样的

展开查看全部
6rqinv9w

6rqinv9w2#

下面是caddyv2通用caddyfile的一些伪代码

对于下面的代码,将"\*"替换为"*""\"在下面用于转义markdown中的"/*"

此代码将向响应添加基本的安全报头和cors
它将代理传递到本地主机端口9883上的进程
如果你有一个dns记录为你的服务器它将设置这letsencrypt证书为你和更新他们当需要
请参阅助手片段https://caddyserver.com/docs/caddyfile/concepts#snippets

  1. # begin common code block snippet to be imported into the server block,
  2. # for example here we set common security headers
  3. # see the markdown escape comment above for "/\*" should be "/*"
  4. (common) {
  5. header /\* {
  6. -Server
  7. -X-Powered-By
  8. +X-Permitted-Cross-Domain-Policies none
  9. +X-Frame-Options DENY
  10. +X-Content-Type-Options nosniff
  11. +Strict-Transport-Security "max-age=63072000 includeSubDomains preload"
  12. +Referrer-Policy no-referrer
  13. }
  14. }
  15. # cors snippet
  16. (cors) {
  17. @cors_preflight method OPTIONS
  18. # "{args.0}" is an input value used when calling the snippet
  19. @cors header Origin "{args.0}"
  20. handle @cors_preflight {
  21. header Access-Control-Allow-Origin "{args.0}"
  22. header Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE"
  23. header Access-Control-Allow-Headers "Content-Type"
  24. header Access-Control-Max-Age "3600"
  25. respond "" 204
  26. }
  27. }
  28. # main server block
  29. # dns record for server is myserver.edu
  30. myserver.edu {
  31. # import common sec headers snippet
  32. import common
  33. # import cors snippet passing server name parameter, wildcard cors poor sec
  34. import cors myserver.edu
  35. # proxy redirect see handle_path directive
  36. # https://caddyserver.com/docs/caddyfile/directives/handle_path
  37. # see the markdown escape comment above for "/\*" should be "/*"
  38. handle_path /somepath/\* {
  39. reverse_proxy localhost:9883 {
  40. header_up X-Real-IP {remote_host}
  41. # caddy will add X-Forwarded-For for you so not need this one
  42. #header_up X-Forwarded-For {remote_host}
  43. header_down Content-Security-Policy "media-src blob:"
  44. }
  45. }
  46. }
展开查看全部

相关问题