Git Product home page Git Product logo

Comments (4)

morozovsk avatar morozovsk commented on July 19, 2024 1

You must fix your config (replace port 8000 to 8004):

ssl on;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/mysite_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.key;

location / {
    proxy_pass http://127.0.0.1:8004;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 3600s; #increase timeout to 1 hour
}

You also must fix yor js code (remove 8004 port or replace to 443):
var ws = new WebSocket('wss://88.99.100.101'); or var ws = new WebSocket('wss://88.99.100.101:443');

True way - add this section to dev-api.mysite.ru.conf instead create new nginx config:

location /websocket {
    proxy_pass http://127.0.0.1:8004;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 3600s; #increase timeout to 1 hour
}

And then you also must fix yor js code:
var ws = new WebSocket('wss://dev-api.mysite.ru/websocket');

from websocket.

morozovsk avatar morozovsk commented on July 19, 2024

Hello. Thank you.
You can use nginx encription for it.
Example: https://github.com/morozovsk/websocket/wiki/Together-with-nginx

from websocket.

ryzhak avatar ryzhak commented on July 19, 2024

Let me show you my config files.

I have a web application. Backend is done via Yii2 and it is using only its rest features. Frontend part is done via Angular.

Backend and frontend are located on different domains and have separate nginx config files with enabled ssl.

Backend nginx config file dev-api.mysite.ru.conf:

`server {
listen 80;
server_name dev-api.mysite.ru;
return 301 https://$server_name$request_uri;
}

server {
charset utf-8;
client_max_body_size 128M;

ssl on;
listen 443 ssl;

server_name dev-api.mysite.ru;
root        /var/www/vhosts/dev-api.mysite.ru/web;
index       index.php;

ssl_certificate /etc/nginx/ssl/mysite_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers  "HIGH:!RC4:!aNULL:!MD5:!kEDH";

access_log  /var/www/vhosts/dev-api.mysite.ru/log/access.log;
error_log   /var/www/vhosts/dev-api.mysite.ru/log/error.log;

location / {
    # Redirect everything that isn't a real file to index.php
    try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    try_files $uri =404;
}

location ~ /\.(ht|svn|git) {
    deny all;
}

}`

Frontend nginx config file dev.mysite.ru.conf:

`server {
listen 80;
server_name dev.mysite.ru;
return 301 https://$server_name$request_uri;
}

server {
charset utf-8;
client_max_body_size 128M;

ssl on;
listen 443 ssl;

root        /var/www/vhosts/dev.mysite.ru;
index       index.html;

server_name dev.mysite.ru;
ssl_certificate /etc/nginx/ssl/mysite_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers  "HIGH:!RC4:!aNULL:!MD5:!kEDH";

access_log  /var/www/vhosts/dev.mysite.ru/log/access.log;
error_log   /var/www/vhosts/dev.mysite.ru/log/error.log;

location / {
    # Redirect everything that isn't a real file to index.php
    try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    try_files $uri =404;
}

location ~ /\.(ht|svn|git) {
    deny all;
}

}`

Now I'm using your library to create a websocket server and apply it to my yii2 application. Everything is fine on my local computer. I can successfully connect to the websocket server via:
var ws = new WebSocket('ws://127.0.0.1:8004');

The next step is to setup this websocket server on a production website. I need this websocket server using SSL.

I create another nginx config file for websocket server ws.mysite.conf:
`server {

ssl on;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/mysite_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.key;

location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 3600s; #increase timeout to 1 hour
}

}`

Yii2 production config for websocket server:
'websocket' => [ 'class' => 'morozovsk\yii2websocket\Connection', 'servers' => [ 'notificationServer' => [ 'class' => 'app\modules\v1\modules\notification\components\WebsocketDaemonHandler', 'pid' => '/tmp/websocket_chat.pid', 'websocket' => 'tcp://127.0.0.1:8004', 'localsocket' => 'tcp://127.0.0.1:8010', //'master' => 'tcp://127.0.0.1:8020', //'eventDriver' => 'event' ] ], ],

And now I'm trying to connect to a websocket server with ssl encryption via:
var ws = new WebSocket('wss://88.99.100.101:8004');
where 88.99.100.101 is an external ip address of mysite.ru.

I got this error: "failed: Error in connection establishment: net::ERR_ADDRESS_UNREACHABLE"

What am I doing wrong?

Thanks in advance. Sorry for the formatting. I don't know why config files are shown not pretty formatted.

from websocket.

ryzhak avatar ryzhak commented on July 19, 2024

Awesome! It worked at last. Thank you very much. The true way you described worked liked a charm.

from websocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.