How to configure and use Nginx server in Node.js
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 and use Nginx server in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

To solve the "Welcome to nginx!" error, you need to check the virtual host configuration, enable the virtual host, reload Nginx, if the virtual host configuration file cannot be found, create a default page and reload Nginx, then the error message will disappear and the website will be normal show.

There are five methods for container communication in the Docker environment: shared network, Docker Compose, network proxy, shared volume, and message queue. Depending on your isolation and security needs, choose the most appropriate communication method, such as leveraging Docker Compose to simplify connections or using a network proxy to increase isolation.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

To register for phpMyAdmin, you need to first create a MySQL user and grant permissions to it, then download, install and configure phpMyAdmin, and finally log in to phpMyAdmin to manage the database.

Converting an HTML file to a URL requires a web server, which involves the following steps: Obtain a web server. Set up a web server. Upload HTML file. Create a domain name. Route the request.

Troubleshooting steps for failed phpMyAdmin installation: Check system requirements (PHP version, MySQL version, web server); enable PHP extensions (mysqli, pdo_mysql, mbstring, token_get_all); check configuration file settings (host, port, username, password); Check file permissions (directory ownership, file permissions); check firewall settings (whitelist web server ports); view error logs (/var/log/apache2/error.log or /var/log/nginx/error.log); seek Technical support (phpMyAdmin
