In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you how to configure Node.js programs to use Nginx server related knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.
Node.js is a platform based on chrome javascript runtime, which is used to build network applications with fast response and easy to expand. Node.js uses the event-driven, non-blocking iCompo model to make it lightweight and efficient, making it ideal for data-intensive real-time applications running on distributed devices, such as real-time chat, and so on. However, gzip encoding, static files, http cache, ssl processing, load balancing and reverse proxy can all be done through nginx, thus reducing the load of node.js, and saving website traffic through nginx's powerful cache to improve the loading speed of the website.
Flow chart
The 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 168k; ssl_certificate / some/location/sillyfacesociety.com.bundle.crt; ssl_certificate_key / some/location/sillyfacesociety.com.key; ssl_protocols sslv3 tlsv1; ssl_ciphers highlands; upstream silly_face_society_upstream {server 127.0.0.1; server 127.0.0.1; 61338; keepalive 64;} server {listen 80; listen 443 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/ | 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 segment description
Http {... Upstream silly_face_society_upstream {server 127.0.0.1 server 61337; keepalive 64;}.}
Nginx load balancer multiple nodo.js instances. Keepalive 64 instructs nginx to maintain a minimum of 64 http/ 1.1 connections to the proxy server at any time. If there is more traffic, nginx will open more connections.
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;}...}}
Send requests that meet which requests to the agent. The matching rules for nginx can be found in the previous article.
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;}.}}
Set up cach
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;...}...}}
Caching is controlled through the http header.
Helloworld
Give it a try. Let's write a helloworld.js.
Var http = require ('http'); http.createserver (function (request, response) {response.writehead (200,{' content-type': 'text/plain'}); response.end (' hello world\ n');}) .birthday (61337); console.log ('server running at http://127.0.0.1:61337/');)
Then open it with the node helloworld.js instruction, so that the nodejs program of the local machine is open, which occupies port 8000 and can be modified by yourself.
At this point, make sure that the settings in the vhost.conf of nginx should be:
Server {listen 80; server_name jb51.net.jb51.net; location / {proxy_pass http://127.0.0.1:61337;}}
Set the domain name of the website, then set the port to 80, and finally set proxy_pass to http://127.0.0.1:61337, passing all requests from jb51.net:80 to the nodejs program.
Restart nginx, access the domain name, and see helloworld.
Although node.js itself can be used as a server is right, for example, welcome.js can be set to port 80.
But a machine runs more than one website, and other websites use other servers. When port 80 has been occupied, it can be handled by proxy to other ports.
These are all the contents of the article "how to configure a Nginx server for Node.js programs". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.