Nginx + (nodejs, socketio, express) + php site

后端 未结 1 2104
不思量自难忘°
不思量自难忘° 2021-01-05 23:10

I\'m working on a fully js, HTML5 canvas game and want it to be \'real-time\'. Based on my research I find out node.js is an exciting prospect, so I configured it on my ubun

相关标签:
1条回答
  • 2021-01-05 23:56

    Finally, my basic problem was configuring the nginx in the right way.

    First I reinstalled nginx as a patched version with nginx_tcp_proxy_module.

    The next step was setting up the right config to handle requests: via http or tcp. I wanted the standard files to be served normally from webroot, just the game logic by node.js (and the socket.io js itself ofc) and .php files by php_fpm.

    So I ended up with the following working nginx setup:

    user  www-data;
    worker_processes  16;
    
    events {
        worker_connections  1024;
    }
    
    http {
        upstream node-js-myapp {
            server 127.0.0.1:3000;
        }
    
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
        keepalive_timeout  65;
        gzip  on;
    
        server {
            listen          80;
            server_name    domain.xx;  # Multiple hostnames seperated by spaces
            root            /var/www/domain.xx; # Replace this
            charset         utf-8;
            access_log  /var/log/nginx/domain.xx.access.log  combined;
            error_log   /var/log/nginx/domain.xx.error.log;
    
            location ~ \.php$ {
                    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name; 
                    include /etc/nginx/conf.d/php_fpm; # Includes config for PHP-FPM (see below)
            }
    
    
            location / {
                index  index.html index.htm;
            }
    
    
            location ^~ /socket.io/ {
                try_files $uri @node-js-myapp;
            }
    
            location /status {
                check_status;
            }
    
            location @node-js-myapp { 
              proxy_set_header X-Real-IP  $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header Host $http_host;
              proxy_pass  http://node-js-myapp;
            }
        }
    }
    
    tcp {
      upstream websocket-myapp {
        server 127.0.0.1:8080;
        check interval=3000 rise=2 fall=5 timeout=1000;
      }
    
      server {
        listen 3000;
        server_name _;
        access_log  /var/log/nginx/domain.xx.access.log;
    
        proxy_read_timeout 200000;
        proxy_send_timeout 200000;
        proxy_pass websocket-myapp;
      }
    }
    

    It's working well with this node.js server:

    var app = require('express').createServer()
    var io = require('socket.io').listen(app);
    io.set('transports', [
        'websocket'
      , 'flashsocket'
      , 'htmlfile'
      , 'xhr-polling'
      , 'jsonp-polling'
      ]);
    
    app.listen(8080);
    

    While the requested file is in the public side of my server and in its HEAD section:

    <script src="/socket.io/socket.io.js"></script>
    

    I'm pretty sure my nginx is not complete and could contain bulls..., but it's kind of working and a good starting point.

    0 讨论(0)
提交回复
热议问题