Search code examples
phpubuntunginxnginx-reverse-proxyreactphp

ReactPhp deploying on Ubunutu 18.04


Hi I have working REST Api with ReactPHP on my local machine I want to deploy it on a dev server ,

I added a newsubdomain on DO, placed a conf with nginx, setup a ongoing process to php index.php my server root file for ReactPHP app.

Allowed the port 8000.

Now I am unable to access my api routes My server log says Listening on tls://127.0.0.1:8000 as default echo on $loop->run();

on hitting the route of subdomain it says

<html>

<head>
    <title>502 Bad Gateway</title>
</head>

<body bgcolor="white">
    <center>
        <h1>502 Bad Gateway</h1>
    </center>
    <hr>
    <center>nginx/1.14.0 (Ubuntu)</center>
</body>

</html>

My nginx conf

server { 
    listen 80;
    server_name test.example.com;
    root /var/www/api; 
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
        proxy_pass             http://127.0.0.1:8000;
        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;
    } 


    listen 443 ssl http2; 
    #ssl info removed
}


Solution

  • After some error and trials made it working

    server { 
    listen 80;
    server_name test.example.com; 
    
    location / {
        proxy_pass             http://127.0.0.1:8000;
        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;
    } 
    
    location /favicon.ico { alias /path/to/favicon.ico; }
    
    listen 443 ssl http2; 
    #ssl info removed
    

    }

    Some points

    • Remove root from server
    • Remove index {We dont need one because ReactPhp has its own Server}
    • Match proxy_pass port with your ReactPhp Server port
    • Its better to provide SSL via proxy with nginx { as ReactPhp Docs}
    • 502 Bad Gateway { It was happening because my server was not running at that moment}