Nginx and Pomelo game server SSL issues
Hello!
I was trying to configure pomelo with nginx but with no success.
Firstly, I added this entry to my hosts file:
127.0.0.1 www.game.localhost gate-1.game.localhost connector-1.game.localhost
Where www is my web-server. gate-1, connector-1 suppose to be reverse proxy to my game server. Let's continue with configuration...
Secondly, I generated self-signed certificate with following commands (correctly configured - https://www.game.localhost worked):
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 1024 -in server.csr -signkey server.key -out server.crt
Thirdly, I configured pomelo as following: servers.json
"gate": [
{
"id": "gate-1",
"host": "127.0.0.1",
"clientPort": 3014,
"frontend": true
}
],
"connector": [
{
"id": "connector-1",
"host": "127.0.0.1",
"port": 3011,
"clientPort": 3015,
"frontend": true
}
]
Fourthly, I configured nginx (basically I configured reverse proxies for ports 80 and secure 443) as following:
server {
listen 80;
server_name www.game.localhost;
location / {
proxy_pass http://localhost:3010;
}
}
server {
listen 443 ssl;
server_name www.game.localhost;
ssl_certificate /xxx/yyy/zzz/server.crt;
ssl_certificate_key /xxx/yyy/zzz/server.key;
location / {
proxy_pass http://localhost:3010;
}
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name gate-1.game.localhost;
access_log off;
location / {
proxy_pass http://localhost:3014;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
server {
listen 443 ssl;
server_name gate-1.game.localhost;
ssl_certificate /xxx/yyy/zzz/server.crt;
ssl_certificate_key /xxx/yyy/zzz/server.key;
access_log off;
location / {
proxy_pass http://localhost:3014;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
server {
listen 80;
server_name connector-1.game.localhost;
access_log off;
location / {
proxy_pass http://localhost:3015;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
server {
listen 443 ssl;
server_name connector-1.game.localhost;
ssl_certificate /xxx/yyy/zzz/server.crt;
ssl_certificate_key /xxx/yyy/zzz/server.key;
access_log off;
location / {
proxy_pass http://localhost:3015;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
}
Fifthly, pomelo is configured with standard "ws" connector, because nginx is already providing certificate and just proxying to pomelo framework.
Now, when I want to connect to the game server, I am able to connect to ws://gate-1.game.localhost:80, but when I try to connect to wss://gate-1.game.localhost:443 is see following message:
WebSocket connection to 'wss://gate-1.game.localhost/' failed: WebSocket opening handshake was canceled
@BonneVoyager
The steps you describe above look good, but it doest work ^^
I think you can capture the tcp packet between nginx and pomelo server and make an analysis based on the packets, and it may be able to indicate where is wrong.
I added debug loggin to gate-1.game.localhost - port 443 and it says:
...
2014/10/30 11:46:29 [info] 5087#0: *1656 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:443
2014/10/30 11:47:06 [info] 5187#0: *1717 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:443
2014/10/30 11:47:07 [info] 5187#0: *1718 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:443
I also add WireShark screenshot with situation where client tries to connect to the server (with all the configuration from above):
I also attach this traffic dump into file https://www.dropbox.com/s/9nis6rz4pky7o6x/proxy-nginx-wss.pcapng?dl=0.
@cynron Would it be possible that nginx is proxying connection to pomelo, but since pomelo is using as "ws", it is not able to recognize client connection?
@BonneVoyager
Sorry to tell you i cant visit dropbox because of f*** GFW now, i will retrieve the pcap file later. according to your snapshot, it seems that nginx never transfer any packet to pomelo server, even the tls handshaking is not complete yet.
I don't have the answer for you right off the bat, but I do have my own Nginx configuration set up for the game I'm building and it works flawlessly with even a staging + production subdomain.
Feel free to use as a reference. I will say that I was running into similar problems before and my issues always ended up being:
- Old Pomelo instance was still running
- Port numbers were not set up right
- /etc/hosts file needed editing
- Nginx needed restarting (
service nginx restart)
Examples from my game Skyduel:
- Pomelo config directory
- Server nginx.conf
- /etc/hosts
@joshjung Thank you for your answer. The thing is that my server is having problems with SSL connection, everything else works well. BTW. Skyduel is a nice game!
@BonneVoyager
Hi,
- I downloaded attached pcap file, and it shows that client closes the connection to nginx as soon as the client succeeds to complete the tls/ssl handshake with nginx. I do not know why, no any https packet is sent from client, even http upgrade packet for websocket.
- according to your test, nginx can proxy http upgrade packet to pomelo server, so it is not problem about http upgrade request(here required by websocket protocol).
- tls/ssl handshake between client and nginx is ok, that means
- certificate issue? You can test a https request for a static html page(not a wss) served by ngnix. if succesful, that means certificate has no problem, but if failing, it would be the problem about certificate.
- if it is certificate issue, you can import your self-signed certificate into brower and make it be root trusted CA, which may help.
Thanks, man :) Out of curiosity, is it possible that with SSL different settings must be used to keep the connection alive?
according to the attached pcap file, I'm sure that it is because self-signed certificate is not trusted by browser, so browser close the connection after handshake, and I think importing the self-signed certificate into the browser as a trusted CA would slove this issue.
@cynron That was it. With this information, I used this command on my mac:
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ./*.game.localhost.cer
... and it works now!
I appreciate your help!
tks all! :)