node.js is a platform based on the chrome javascript runtime, used to easily build web applications with fast response speed and easy expansion. Node.js uses an event-driven, non-blocking I/O model to be lightweight and efficient. It is very suitable for data-intensive real-time applications running on distributed devices, such as real-time chat and so on. However, gzip encoding, static files, http caching, SSL processing, load balancing and reverse proxy, etc., can all be completed through nginx, thereby reducing the load on node.js and saving website traffic through nginx's powerful cache. Improve website loading speed.
Flowchart
##nginx configuration is as follows:
http { proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m; proxy_temp_path /var/tmp; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; gzip_comp_level 6; gzip_vary on; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k; ssl_certificate /some/location/sillyfacesociety.com.bundle.crt; ssl_certificate_key /some/location/sillyfacesociety.com.key; ssl_protocols sslv3 tlsv1; ssl_ciphers high:!anull:!md5; upstream silly_face_society_upstream { server 127.0.0.1:61337; server 127.0.0.1:61338; keepalive 64; } server { listen 80; listen 443 ssl; server_name sillyfacesociety.com; return 301 $scheme://www.sillyfacesociety.com$request_uri; } server { listen 80; listen 443 ssl; server_name www.sillyfacesociety.com; error_page 502 /errors/502.html; location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) { root /usr/local/silly_face_society/node/public; access_log off; expires max; } location /errors { internal; alias /usr/local/silly_face_society/node/public/errors; } location / { proxy_redirect off; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header x-forwarded-proto $scheme; proxy_set_header host $http_host; proxy_set_header x-nginx-proxy true; proxy_set_header connection ""; proxy_http_version 1.1; proxy_cache one; proxy_cache_key sfs$request_uri$scheme; proxy_pass http://silly_face_society_upstream; } } }
Configuration Section Description
http { ... upstream silly_face_society_upstream { server 127.0.0.1:61337; server 127.0.0.1:61338; keepalive 64; } ... }
http { ... server { ... location / { proxy_redirect off; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_set_header host $http_host; proxy_set_header x-nginx-proxy true; ... proxy_set_header connection ""; proxy_http_version 1.1; proxy_pass http://silly_face_society_upstream; } ... } }
nginx handles static content
http { ... server { ... location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) { root /usr/local/silly_face_society/node/public; access_log off; expires max; } ... } }
http { ... proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m; proxy_temp_path /var/tmp; ... } http { server { ... location / { ... proxy_cache one; proxy_cache_key sfs$request_uri$scheme; ... } ... } }
helloworld
Let’s try it out. Let’s write helloworld.js
var http = require('http'); http.createserver(function (request, response) { response.writehead(200, {'content-type': 'text/plain'}); response.end('hello world\n'); }).listen(61337); console.log('server running at http://127.0.0.1:61337/');
server { listen 80; server_name jb51.net.jb51.net; location / { proxy_pass http://127.0.0.1:61337; } }
Restart nginx, access the domain name, and you can see helloworld.
Although it is true that node.js itself can be used as a server, for example, just set port 80 in welcome.js.
But one machine runs multiple websites, and other websites use other servers. When port 80 is already occupied, it can be processed by proxy to other ports.
The above is the detailed content of How to configure a Node.js application to use an Nginx server. For more information, please follow other related articles on the PHP Chinese website!