boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
10.04k stars 709 forks source link

nginx config for server #322

Open pociej opened 5 years ago

pociej commented 5 years ago

Im trying to setup server on DigitalOcean and im really not sure how should i config nginx to make this working. I will share if i will find the way but maybe someone can help by providing instruction how to config nginx for this case? Don't you that would be nice to add some deployment instructions to docs?

pociej commented 5 years ago

I opened question with details on stackoverflow : https://stackoverflow.com/questions/53866211/nginx-config-for-socket-io

EDIT: above issue issue is solved in this sense my problem with nginx config for socket.io was my fault. Anyways im still unable to run game server on production.

My current nginx config is : server { listen 80 default_server; listen [::]:80 default_server; servername ;

 location / {
     proxy_pass http://localhost:3031;
     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 /socket.io/ {
    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_set_header X-NginX-Proxy true;
    proxy_pass http://localhost:3030;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
 }

}

my server is as simple as possible :

import { Game } from "./lib/game.js";
import { Server, Mongo } from "boardgame.io/server";

const server = Server({
  games: [Game],

  db: new Mongo({
    url: "my_mongo_url",
    dbname: "zz"
  })
});

server.run(3030);

and my client is :

export const App = props => {
  const CmClient = Client({
    game: Game,
    board: GameBoard,
    numPlayers: 1,
    enhancer: applyMiddleware(middleware(props.playerId)),
    multiplayer: {
      server: `http://157.230.24.193`
    }
  });
  return (
    <div>
      <CmClient gameID={props.playerId} playerID="0" />
    </div>
  );
};

I see no errors in console related to CORS or 404 but still game is not connected to server properly i dont see any changes in db. Of course same code on localhost with two separated ports works, and above issue seems to game agnostic, same effect i have when i run using tictactoe. @nicolodavis could you take a quick look at this please? I would match appreciate such a help.

vdfdev commented 5 years ago

I have a working nginx for turnato.com. I will send it soon here

nicolodavis commented 5 years ago

A deployment section is definitely lacking in the docs. Can compile one from the info on this thread and expand it later with other configurations.

vdfdev commented 5 years ago

I recommend setting up a SSL protocol on your nginx. I recommend LetsEncrypt. Besides that, your nginx configuration file seems right. Also, I do not overwrite the server URL in turnato, as it is running on the same domain and the default ports.

This is the full nginx configuration file for turnato.com:

server {

    server_name turnato.com;

    location / {
        proxy_pass http://localhost:8001;
        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 /blog {
        autoindex on;
        root /home/turnato-server;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/turnato.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/turnato.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    server_name www.turnato.com;
    return 301 https://turnato.com;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/turnato.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/turnato.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.turnato.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = turnato.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;

    server_name turnato.com www.turnato.com;
    return 404; # managed by Certbot
}
pociej commented 5 years ago

@flamecoals thx a lot for reply. In regards to SSL i don't care about security now, do it have any other impact? Also our confings are different because i have separate location /socket.io/. Look in server implementation : https://github.com/nicolodavis/boardgame.io/blob/master/src/server/index.js#L44-L45 we see that on two subsequent ports boardgames.io runs socket and rest api. When i omit socket.io location, when my client starts i see :

GET http://157.230.24.193/socket.io/?EIO=3&transport=polling&t=MVB0n6X&b64=1 404 (Not Found)

I wonder why you dont have same problem with your nginx. Could you please show me @flamecoals your client and server as i did in second post in this thread ?

vdfdev commented 5 years ago

What is the domain you are running the nginx ? Because if you try to connect to another domain (157.230.24.193) you are probably going to get a cross domain security exception ? That might be it?

With that said, I am currently not leveraging the REST API from boardgame.io. I should soon, so I will keep this up to date if I face the same issues.

vdfdev commented 5 years ago

@pociej The server is here: https://github.com/flamecoals/turnato/blob/master/src/server.tsx#L58

and the client is here: https://github.com/flamecoals/turnato/blob/master/src/components/games/chess/index.tsx#L38

pociej commented 5 years ago

@flamecoals thx for what you shared but it seems not related to what i asked for. Look in your client multiplayer: true i don't know what is effect of such setup as there is no such way in documentation, you can set multiplayer: { local: true }, or multiplayer: { server: my_server_uri}, i guess what you did is using some legacy version of boardgame.io or its shorthand notation for multiplayer : { local : true }. In your nginx config you have nothing related to socket.io so it if it works indeed im pretty sure its like i guessed that your master is running in browser.

vdfdev commented 5 years ago

@pociej, it seems like multiplayer: true will connect to the URL, which is nice as I dont need to worry about getting the right port/url etc. Just run the server and multiplayer: true takes care of the rest. { local: true } is more for testing, as it uses a global object to communicate between two instances of the game on the same page.