centrifugal / centrifugo

Scalable real-time messaging server in a language-agnostic way. Self-hosted alternative to Pubnub, Pusher, Ably. Set up once and forever.
https://centrifugal.dev
Apache License 2.0
8.45k stars 598 forks source link

nginx centrifuge is not working #491

Closed bagasik closed 3 years ago

bagasik commented 3 years ago

Hello. nginx centrifuge is not working. My system centos-release-7-6.1810.2.el7.centos.x86_64. Centrifugo v3.1.0 (Go version: go1.17.3)

The ip address opens 1.1.1.1:9000 It doesn't open https://my_site.io/connection/websocket ( in browser error Bad Request ). log nginx 12.4.12.78 - - [11/Nov/2021:14:11:26 +0100] "GET /connection/websocket HTTP/1.1" 400 12 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0"

What am I doing wrong?

cat /etc/centrifugo/config.json
{
  "token_hmac_secret_key": "my",
  "admin_password": "my",
  "admin_secret": "my",
  "api_key": "my",
  "port": 9000,
  "admin": true
}
cat /lib/systemd/system/centrifugo.service 
[Unit]
Description=Centrifugo services
After=network.target syslog.target

[Service]
User=centrifugo
Group=centrifugo
LimitNOFILE=55555

Environment="CONFIG=/etc/centrifugo/config.json"
ExecStartPre=/usr/bin/centrifugo checkconfig -c $CONFIG
ExecStart=/usr/bin/centrifugo -c $CONFIG
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -SIGTERM $MAINPID
TimeoutStopSec=10
KillMode=control-group
RestartSec=1
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=centrifugo

[Install]
WantedBy=multi-user.target
Alias=centrifugo.service
# systemctl status centrifugo
● centrifugo.service - Centrifugo services
   Loaded: loaded (/usr/lib/systemd/system/centrifugo.service; enabled; vendor preset: disabled)
   Active: active (running) since Чт 2021-11-11 13:51:58 CET; 7min ago
  Process: 28567 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 30498 ExecStartPre=/usr/bin/centrifugo checkconfig -c $CONFIG (code=exited, status=0/SUCCESS)
 Main PID: 30511 (centrifugo)
   CGroup: /system.slice/centrifugo.service
           └─30511 /usr/bin/centrifugo -c /etc/centrifugo/config.json

ноя 11 13:51:58 n50 systemd[1]: Starting Centrifugo services...
ноя 11 13:51:58 n350 systemd[1]: Started Centrifugo services.
ноя 11 13:51:58 n350 centrifugo[30511]: {"level":"info","version":"3.1.0","runtime":"go1.17.3","pid":30511,"engine":"Memory","gomaxprocs":8,"time":...trifugo"}
ноя 11 13:51:58 n350 centrifugo[30511]: {"level":"info","path":"/etc/centrifugo/config.json","time":"2021-11-11T13:51:58+01:00","message":"using config file"}
ноя 11 13:51:58 n350 centrifugo[30511]: {"level":"info","algorithms":"HS256, HS384, HS512","time":"2021-11-11T13:51:58+01:00","message":"enabled JW...rifiers"}
ноя 11 13:51:58 n350 centrifugo[30511]: {"level":"info","time":"2021-11-11T13:51:58+01:00","message":"serving websocket, API, admin endpoints on :9000"}
Hint: Some lines were ellipsized, use -l to show in full.
cat /etc/nginx/vhosts/my_site/my_site.io.conf
upstream centrifugo {
        ip_hash;
        server 127.0.0.1:9000;
}

map $http_upgrade $connection_upgrade {
        default upgrade;
            '' close;
}

upstream websocket {
        server 1.1.1.1:8000;
}

server {
        listen 1.1.1.1:80;
        root /usr/local/www/default;
}

server {
    listen 1.1.1.1:80;
    server_name my_site.io my_site.online;
    charset off;
    index index.php index.html;
    disable_symlinks if_not_owner from=$root_path;
    ssi on;
    set $root_path /var/www/my_site.io/;
    root $root_path;

location / {
    try_files $uri $uri/ =404;
}

location /p {
          rewrite /p/(.*) /$1 break;
          proxy_pass http://my_site.io/ref.php?id=$1;
          proxy_redirect     off;
          proxy_set_header   Host $host;
}

location /centrifugo/ {
        rewrite ^/centrifugo/(.*)        /$1 break;
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://centrifugo;
}

location /connection/websocket {
        rewrite ^/centrifugo(.*)        $1 break;
        proxy_pass http://centrifugo;
        proxy_buffering off;
        keepalive_timeout 65;
        proxy_read_timeout 60s;
        proxy_http_version 1.1;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
}

location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
    expires 5d;
}

location /websocket {
    proxy_pass http://1.1.1.1:8000/;
    proxy_redirect http://1.1.1.1:8000/ /websocket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    access_log /var/www/httpd-logs/my_site-ssl-websocket.access.log;

}

location ^~ /\?hash {
    proxy_pass http://1.1.1.1:8000/;
    proxy_redirect http://1.1.1.1:8000/ /;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~ \.php$ {
        fastcgi_pass   unix:/var/www/php-fpm/my_site.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index  index.php;
        include fastcgi_params;
        fastcgi_param PHP_ADMIN_VALUE "sendmail_path = /usr/sbin/sendmail -t -i -f webmaster@my_site.io";
        fastcgi_split_path_info ^((?U).+\.ph(?:p\d*|tml))(/?.+)$;
        try_files $uri =404;
}
}
FZambia commented 3 years ago

@bagasik hello! Check out this example with working Nginx configuration. Run with docker compose up and open http://localhost:9000/chat/ which will be served by Nginx.

Make sure you have a proper allowed_origins configured in Centrifugo. And check out Centrifugo logs.

Closing – sorry, I can't really help here – it's a bit offtopic for the issue tracker. Try asking for the help in our community rooms.

bagasik commented 3 years ago

It's bad, I went through all the possible settings, not one does not work.

FZambia commented 3 years ago

Join community channels and we can try to find a reason.

bagasik commented 3 years ago

Where to find these community channels?

mustafagunel commented 8 months ago

https://centrifugal.dev/docs/getting-started/community