Tuesday 15 January 2013

node.js - Install Nodejs app on server running on apache which uses reverse proxy with nginx -



node.js - Install Nodejs app on server running on apache which uses reverse proxy with nginx -

i know there many such questions on stack exchange. nil help scenario have.

here situation.

i have webserver running on apache2 listening port numbers 7080 , 7081. have used reverse-proxy method on server , installed nginx listening port 80. nginx front end end. have wordpress website running on http://www.example.com.

now trying install node.js app on server not. makes sense because port 80 beingness used nginx.

i referred next posts on so

node.js + nginx - , now?

apache , node.js on same server

i tried following

upstream example.com/my-app { server 1**.*.**.**:3010; } # nginx server instance server { hear 1**.*.**.**:80; server_name example.com/my-app; server_name www.example.com/my-app; server_name ipv4.example.com/my-app; access_log off; # pass request node.js server right headers , much more can added, see nginx config options location / { proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-accel-internal /internal-nginx-static-location; proxy_pass http://example.com/my-app; proxy_redirect off; } location /internal-nginx-static-location/ { alias /var/www/vhosts/example.com/httpdocs/node; access_log /var/www/vhosts/example.com/httpdocs/node/statistics/logs/proxy_access_ssl_log; add_header x-powered-by plesklin; internal; } }

i wrote above conf in file , included in /etc/nginx/conf.d/xzzeaweae_nginx.conf.

it not working. app running on 1++.+.++.++:3010 though.

my directory structure.

/var/www/vhosts/example.com/httpdocs/

my wordpress website root directory : /var/www/vhosts/example.com/httpdocs/

my nodejs app directory: /var/www/vhosts/example.com/httpdocs/my-nodejsapp-folder/

update

here reverse proxy config apache application

server { hear +++.+.++.++:80 ; hear ++.+.+++.++:80 ; location / { proxy_pass http://127.0.0.1:7080; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; } }

since have more 1 website running on server,

i have reverse proxy config every website.

here 1 of website

server { hear +++.+.++.++:443 ssl; server_name example.com; server_name www.example.com; server_name ipv4.example.com; ssl_certificate /opt/psa/var/certificates/certaqnxhd2; ssl_certificate_key /opt/psa/var/certificates/certaqnxhd2; ssl_session_timeout 5m; ssl_protocols sslv2 sslv3 tlsv1; ssl_ciphers high:!anull:!md5; ssl_prefer_server_ciphers on; client_max_body_size 128m; location / { # ipv6 isn't supported in proxy_pass yet. proxy_pass https://+++.+.++.++:7081; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-accel-internal /internal-nginx-static-location; access_log off; } location /internal-nginx-static-location/ { alias /var/www/vhosts/example.com/httpdocs/; access_log /var/www/vhosts/example.com/statistics/logs/proxy_access_ssl_log; add_header x-powered-by plesklin; internal; } } server { hear +++.+.++.++:443 ssl; server_name webmail.example.com; ssl_certificate /opt/psa/var/certificates/certaqnxhd2; ssl_certificate_key /opt/psa/var/certificates/certaqnxhd2; ssl_session_timeout 5m; ssl_protocols sslv2 sslv3 tlsv1; ssl_ciphers high:!anull:!md5; ssl_prefer_server_ciphers on; client_max_body_size 128m; location / { # ipv6 isn't supported in proxy_pass yet. proxy_pass https://+++.+.++.++:7081; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; access_log /var/www/vhosts/example.com/statistics/logs/webmail_access_ssl_log; } } server { hear +++.+.++.++:80; server_name example.com; server_name www.example.com; server_name ipv4.example.com; client_max_body_size 128m; location / { # ipv6 isn't supported in proxy_pass yet. proxy_pass http://+++.+.++.++:7080; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-accel-internal /internal-nginx-static-location; access_log off; } location /internal-nginx-static-location/ { alias /var/www/vhosts/example.com/httpdocs/; access_log /var/www/vhosts/example.com/statistics/logs/proxy_access_log; add_header x-powered-by plesklin; internal; } } server { hear +++.+.++.++:80; server_name webmail.example.com; client_max_body_size 128m; location / { # ipv6 isn't supported in proxy_pass yet. proxy_pass http://+++.+.++.++:7080; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; access_log /var/www/vhosts/example.com/statistics/logs/webmail_access_log; } }

note: sites-available , sites-enabled files nowadays within apache2. not in nginx.

i want nodejs app run on example.com/my-nodejsapp-folder/ without port number.

any help highly appreciated.

http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream

i haven't seen says can utilize dots , slashes in upstream name

upstream mynodeapp { server 1**.*.**.**:3010; }

then

server { hear 1**.*.**.**:80; server_name example.com/my-app; #...etc. location / { proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; # not this. # proxy_set_header x-accel-internal /internal-nginx-static-location; proxy_pass http://mynodeapp/my-app; proxy_redirect off; } }

then node app needs write header containing:

x-accel-redirect: /internal-nginx-static-location/somefile

there restrictions, in, may not work if start returning content (e.g. print statements) before returning headers. it's simpler first test interesting header.

example: # /etc/nginx/conf.d/default.conf upstream mynodeapp { server 127.0.0.1:8000; } server { hear 127.0.0.1:80; location /secret { alias /tmp/secret; internal; } location /my-app { proxy_pass http://mynodeapp/my-app; } }

and let's seek following:

// /tmp/index.js var http = require('http'); http.createserver(function (req, res) { res.writehead(200, {'x-accel-redirect': '/secret/foo'}); res.end('hello world\n'); }).listen(8000, '127.0.0.1');

and command line:

[root@localhost secret]# pwd /tmp/secret [root@localhost secret]# echo bar > foo [root@localhost secret]# curl http://127.0.0.1:80/my-app bar [root@localhost secret]# curl http://127.0.0.1:80/secret/foo <html> <head><title>404 not found</title></head> <body bgcolor="white"> <center><h1>404 not found</h1></center> <hr><center>nginx/1.0.15</center> </body> </html> [root@localhost secret]#

node.js apache nginx proxy plesk

No comments:

Post a Comment