dunglas / mercure

🪽 An open, easy, fast, reliable and battery-efficient solution for real-time communications
https://mercure.rocks
GNU Affero General Public License v3.0
3.98k stars 296 forks source link

Deploy mercure with Caddy on my Nginx web server #456

Closed Lyro1 closed 3 years ago

Lyro1 commented 3 years ago

Hello there,

I have been developing a web app using Mercure Hub, and faced no issue in my local environment. I now got a server on which I set up a Nginx Web Server so that I could host my app.

The app is made of 3 blocks, each block having its own Nginx space:

Back and front are working as expected.

But, when I tried to setup Mercure on my web server, I create its Nginx space with the following setup:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    include snippets/my-cert.conf;
    include snippets/ssl-params.conf;

    server_name my-app-mercure.mydomain.com www.my-app-mercure.mydomain.com;

    index index.php;
    client_max_body_size 100m;

    location / {
        proxy_pass http://127.0.0.1:3001;
    proxy_read_timeout 24h;
    proxy_http_version 1.1;
    proxy_set_header Connection "";

        ## Be sure to set USE_FORWARDED_HEADERS=1 to allow the hub to use those headers ##
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server {
    listen 80;
    listen [::]:80;

    server_name my-app-mercure.mydomain.com www.my-app-mercure.mydomain.com;

    return 302 https://$server_name$request_uri;
}

I use Mercure Hub in 0.11, so it uses a CaddyServer. My Caddyfile is like so:

# Learn how to configure the Mercure.rocks Hub on https://mercure.rocks/docs/hub/config
{
    # Debug mode (disable it in production!)
    {$DEBUG}
    # HTTP/3 support
    servers {
        protocol {
            experimental_http3
        }
    }
}

{$SERVER_NAME:localhost}

log

route {
    encode zstd gzip

    mercure {
        # Transport to use (default to Bolt)
        transport_url {$MERCURE_TRANSPORT_URL:bolt://mercure.db}
        # Publisher JWT key
        publisher_jwt *my_jwt_token*
        # Subscriber JWT key
        subscriber_jwt *my_jwt_token*
        # Extra directives
        {$MERCURE_EXTRA_DIRECTIVES}
    }

    respond /healthz 200

    respond "Not Found" 404
}

When I try to run mercure, I get the following error:

louis@lyro:/var/www/my-app-back.mydomain.com/mercure$ sudo ./mercure run
2021/01/22 10:50:36.906 INFO    using adjacent Caddyfile
2021/01/22 10:50:36.908 INFO    admin   admin endpoint started  {"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["127.0.0.1:2019", "localhost:2019", "[::1]:2019"]}
2021/01/22 10:50:36.910 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}
2021/01/22 10:50:36.910 INFO    http    enabling automatic HTTP->HTTPS redirects    {"server_name": "srv0"}
2021/01/22 10:50:36.921 INFO    tls.cache.maintenance   started background certificate maintenance  {"cache": "0xc00043ad20"}
run: loading initial config: loading new config: http app module: start: tcp: listening on :443: listen tcp :443: bind: address already in use

Which is normal because Nginx uses port 443. How can I use Nginx with the CaddyServer on this new Mercure Hub version?

leekung commented 3 years ago

The SERVER_NAME must be full URL to make Mercure listen on http only example

USE_FORWARDED_HEADERS=1 MERCURE_PUBLISHER_JWT_KEY='!xxx!' MERCURE_SUBSCRIBER_JWT_KEY='!xxx!' SERVER_NAME='http://127.0.0.1:3001' ./mercure run -config Caddyfile

or change the line {$SERVER_NAME:localhost} to http://127.0.0.1:3001

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.