问题
I know there are several questions like this but i can't find one solving my problem for a project which is not in the server root. I have the following situation: I have an Apache server which runs several projects like:
0.0.0.0/project1
0.0.0.0/project2
I added a node.js project let's call it nodeproject
. With ProxyPass I am passing requests made to 0.0.0.0/nodeproject
to 0.0.0.0:8080
where my node server is running. This is all working fine except the websocket configuration for socket.io.
In my /etc/apache2/apache2.conf
file i placed this code to do the proxying. mod_proxy_wstunnel
is enabeld Which i basically copied from here.
ProxyRequests off
ProxyVia on
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/nodeproject/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:8080/$1 [P,L]
ProxyPass /nodeproject/socket.io http://localhost:8080/socket.io
ProxyPassReverse /nodeproject/socket.io http://localhost:8080/socket.io
<Location /nodeproject>
ProxyPass http://localhost:8080
ProxyPassReverse http://localhost:8080
</Location>
The failing request looks like this:
ws://0.0.0.0/nodeproject/socket.io/?EIO=3&transport=websocket&sid=MDuVcHmt3T1sa8ruAAAa
and i get a Bad Request 400 response.
I am not an expert in configuring this kind of stuff where did I go wrong?
Thanks in advance!
回答1:
You shouldn't put the rewrite conditional in the /etc/apache2/apache2.conf
, but in the virtual host directive in /etc/apache2/sites-available/000-project-whatever.conf
.
Here's an example from one of my own little projects.
<VirtualHost *:80>
ServerName sockey.api
ServerAdmin webmaster@localhost
DocumentRoot /var/www/sockey.api
<Directory /var/www/sockey.api>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorDocument 404 /index.html
ErrorLog ${APACHE_LOG_DIR}/sockey.api.error.log
CustomLog ${APACHE_LOG_DIR}/sockey.api.access.log combined
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:8081/$1 [P,L]
ProxyPass /socket.io http://localhost:8081/socket.io
ProxyPassReverse /socket.io http://localhost:8081/socket.io
</VirtualHost>
回答2:
I was able to use Apache as a proxy for socket.io using this configuration:
RewriteEngine On
RewriteCond %{QUERY_STRING} transport=polling [OR]
RewriteCond %{REQUEST_URI} /socket.io/socket.io.js
RewriteRule /socket.io/(.*)$ http://localhost:8082/socket.io/$1 [P]
ProxyPass /socket.io/ ws://localhost:8082/socket.io/
ProxyPassReverse /socket.io/ ws://localhost:8082/socket.io/
It takes into account that there are two types of requests going over https: the request for the js file socket.io.js
itself, and the polling requests.
来源:https://stackoverflow.com/questions/36472920/apache-proxy-configuration-for-socket-io-project-not-in-root