Score:0

how to set up websocket server along with two laravel backend and a spa

fr flag

i have two laravel backend setup on different routes. and there is a spa on root. now i want to set a websocket server along with it. here is my website.conf


############## block-4 : multiple subdirectory testing ############
server {
    listen 80;
    #    server_name abc.xyz;
    server_name _;
    root /var/www/html/abc.xyz;
    #    root /var/www/html;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.php;

 
    location / {
        # root /var/www/html/abc.xyz;
        try_files $uri  /index.html ;
    }

    charset utf-8;

    location = /favicon.ico {
        access_log off; log_not_found off;
    }
    location = /robots.txt {
        access_log off; log_not_found off;
    }

    # error_page 404 /index.php;

############## block-4 : multiple subdirectory testing ############
server {
    listen 80;
    #    server_name abc.xyz;
    server_name _;
    root /var/www/html/abc.xyz;
    #    root /var/www/html;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.php;

 
    location / {
        # root /var/www/html/abc.xyz;
        try_files $uri  /index.html ;
    }

    charset utf-8;

    location = /favicon.ico {
        access_log off; log_not_found off;
    }
    location = /robots.txt {
        access_log off; log_not_found off;
    }

    # error_page 404 /index.php;

    # BACKEND location rewrite instructions
    location /backend {
        alias /var/www/html/abc.xyz/backend;

        try_files $uri $uri/ @backend;

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        }
    }

    location @backend {
        # rewrite /backend/(.*)$ /backend/index.php?/$1 last;
        rewrite ^/backend/(.*)$ /backend/index.php last;
    }
    # end of the BACKEND location


    # BACKEND location rewrite instructions
    location /api {
        alias /var/www/html/abc.xyz/api;

        try_files $uri $uri/ @api;

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        }
    }

    location @api {
        rewrite /api/(.*)$ /api/index.php?/$1 last;
    }
    # end of the BACKEND location


    #  phpmyadmin rewrite rules.
    location /phpmyadmin {
        root /usr/share/;
        index index.php index.html index.htm;
        location ~ ^/phpmyadmin/(.+\.php)$ {
            try_files $uri =404;
            root /usr/share/;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;
        }

        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
            root /usr/share/;
        }
    }
    # end of phpmyadmin block here.

    # web socket configuration here 
  location /ws* {
    proxy_pass             http://127.0.0.1:6001;
    proxy_set_header Host  $host;
    proxy_read_timeout     60;
    proxy_connect_timeout  60;
    proxy_redirect         off;

    # Allow the use of websockets
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
    # end of websocket configuration


    location ~ /\.(?!well-known).* {
        deny all;
    }
}

############# end block-4 ######################


the location /ws block is taken from laravel websockets documentation. but this location block is giving 404 not found error. but this nginx configuration doesn't work. My laravel website project works fine on localhost.

edit

this is the output of lsof -i :80,443,6001

COMMAND    PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
php     414490     root    5u  IPv4 5974385      0t0  TCP *:x11-1 (LISTEN)
php     414490     root    6u  IPv4 5991457      0t0  TCP localhost.localdomain:x11-1->localhost.localdomain:35684 (ESTABLISHED)
nginx   415533     root    6u  IPv4 5990824      0t0  TCP *:https (LISTEN)
nginx   415533     root    7u  IPv4 5990825      0t0  TCP *:http (LISTEN)
nginx   415534 www-data    6u  IPv4 5990824      0t0  TCP *:https (LISTEN)
nginx   415534 www-data    7u  IPv4 5990825      0t0  TCP *:http (LISTEN)
nginx   415535 www-data    6u  IPv4 5990824      0t0  TCP *:https (LISTEN)
nginx   415535 www-data    7u  IPv4 5990825      0t0  TCP *:http (LISTEN)
nginx   415536 www-data    6u  IPv4 5990824      0t0  TCP *:https (LISTEN)
nginx   415536 www-data    7u  IPv4 5990825      0t0  TCP *:http (LISTEN)
nginx   415537 www-data    6u  IPv4 5990824      0t0  TCP *:https (LISTEN)
nginx   415537 www-data    7u  IPv4 5990825      0t0  TCP *:http (LISTEN)
nginx   415538 www-data    6u  IPv4 5990824      0t0  TCP *:https (LISTEN)
nginx   415538 www-data    7u  IPv4 5990825      0t0  TCP *:http (LISTEN)
nginx   415539 www-data    6u  IPv4 5990824      0t0  TCP *:https (LISTEN)
nginx   415539 www-data    7u  IPv4 5990825      0t0  TCP *:http (LISTEN)
nginx   415539 www-data    8u  IPv4 5985866      0t0  TCP linux:https->157.42.56.21:55728 (ESTABLISHED)
nginx   415539 www-data   12u  IPv4 5985868      0t0  TCP localhost.localdomain:35684->localhost.localdomain:x11-1 (ESTABLISHED)
djdomi avatar
za flag
Which port is the Laravel listen to?
rajesh_chaurasiya avatar
fr flag
@djdomi are you asking for laravel default http port or websocket port? laravel default port 80 and for websocket it is 6001.
djdomi avatar
za flag
show us, `lsof -i :80,443,6001`
rajesh_chaurasiya avatar
fr flag
@djdomi i am adding the output in the question edit.
djdomi avatar
za flag
maybe You can join [thechat](https://chat.stackexchange.com/rooms/126791/thechat) to shorten it, but i can only see NGINX running on these 2 ports. - but i see you are running a newer version of lsof so `lsof -P -i :80,443,6001` might be updated :)
rajesh_chaurasiya avatar
fr flag
@djdomi i have moved away from this nginx configuration. this time i created server block for websocket on a different subdomain. that worked. however that is still not working properly as expected. but i want to try it.
djdomi avatar
za flag
@rajeshchaurasiya Well subdomains IMHO are better for such thing instead of subdirectories. But Maybe you have just forgotten to reload nginx and this time you did it due to the change to a subdomain? :)
rajesh_chaurasiya avatar
fr flag
@djdomi no sir. i remember this rule. i had reloaded and restarted( for extra precaution) the nginx configuration. i think problem was in the location block perhaps. but thank you sir for your kind attention. i will be in touch
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.