Adding chat through websocket, to an existing PHP web app

人走茶凉 提交于 2020-01-05 05:37:06

问题


I have an existing web application on the standard PHP/Apache platform. Now, what I want is to add chat functionality, and I want it to be real-time over a websocket, and to that extend I've studied socket.io on nodejs a little bit. So in addition to Apache running the big PHP app, I would have nodejs with socket.io running chat.

But what I don't really understand, is how would I recognise my users in the code for nodejs chat? For one, Apache and nodejs won't be able to run on the same port, meaning I'll run chat on port 8080 for example, in which case I lose the user's cookies, which then means I now have to ask them to log in once more on this nodejs-powered port if they want to use chat? Seems ridiculous, but I don't know which way to go about it.

I cannot port my entire code onto nodejs, of course. So ideally I would want Apache and nodejs to coexist. Or I am just completely misunderstanding how chat is supposed to work in web apps.

Any tips appreciated.


回答1:


You can run your Apache with PHP on e.g. port 3001 and your Node app on port 3002, and have nginx configured as a reverse proxy to make them both available on port 80, for example your PHP app in the root / directory and your Node app in the /chat directory, with nginx config like this:

server {
    listen 80;
    server_name example.com;
    location /chat {
        proxy_pass http://localhost:3002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
}

With SSL it would be slightly more complicated but not that much:

server {
    listen 443;
    server_name example.com;
    add_header Strict-Transport-Security "max-age=3600";
    ssl on;
    ssl_certificate /.../chained2.pem;
    ssl_certificate_key /.../domain.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA;
    ssl_session_cache shared:SSL:50m;
    ssl_prefer_server_ciphers on;

    location /chat {
        proxy_pass http://localhost:3002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }

}

Your PHP and Node apps could even run on different servers - just use their addresses in the nginx config.

See this answer and its comments for more details:

  • reverse proxy using ngix and ssl implementation on express failed


来源:https://stackoverflow.com/questions/41316681/adding-chat-through-websocket-to-an-existing-php-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!