Nginx configuration code list: listening port, access logs, domain name, static assets, redirection, reverse proxy, load balancing and SSL protocol

888u

Last update at :2024-01-19,Edit by888u

What does Nginx mean? Nginx is a high-performance HTTP and reverse proxy web server that also provides IMAP/POP3/SMTP services. It is popular among developers due to its rich feature set, stability, sample configuration files and low system resource consumption. .

In this article, we summarize some commonly used Nginx configuration codes, including: listening port, access log, domain name, static assets, redirection, reverse proxy, load balancing and SL protocol. We hope it will be helpful to everyone.

Nginx listening port

server {# Standard HTTP Protocol 80;# Standard HTTPS Protocol 443 ssl;# For http2listen 443 ssl http2;# Listen on 80 using IPv6listen [::]:80; ;}

Nginx access log

server {# Relative or full path to log fileaccess_log /path/to/file.log;# Turn 'on' or 'off' access_log on;}

Nginx domain name

server {# Listen to yourdomain.comserver_name yourdomain.com;# Listen to multiple domains server_name yourdomain.com www.yourdomain.com;# Listen to all domainsserver_name *.yourdomain.com; # Listen to unspecified Hostnames (Listens to IP address itself)server_name "";}

Nginx static assets

server {listen 80;server_name yourdomain.com;location / {root /path/to/website;}}

Nginx redirect

server {listen 80;server_name www.yourdomain.com;return 301 http://yourdomain.com$request_uri;}server {listen 80;server_name www.yourdomain.com;location /redirect-url {return 301 http://otherdomain .com;}}

Nginx reverse proxy

server {listen 80;server_name yourdomain.com;location / {proxy_pass http://0.0.0.0:3000;# where 0.0.0.0:3000 is your application server (Ex: node.js) bound on 0.0.0.0 listening on port 3000}}

Nginx load balancing

upstream node_js {server 0.0.0.0:3000;server 0.0.0.0:4000;server 123.131.121.122;}server {listen 80;server_name yourdomain.com;location / {proxy_pass http://node_js;}}

Nginx SSL protocol

server {listen 443 ssl;server_name yourdomain.com;ssl on;ssl_certificate /path/to/cert.pem;ssl_certificate_key /path/to/privatekey.pem;ssl_stapling on;ssl_stapling_verify on;ssl_trusted_certificatechain/path/to/fullkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_session_timeout 1h;ssl_session_cache shared:SSL:50m;add_header Strict-Transport-Security max-age=15768000;}# Permanent Redirect for HTTP to HTTPSserver {listen 80;server er_name yourdomain.com;return 301 https://$host$request_uri;}

In fact, Nginx can be configured visually. Our blog found an artifact on GitHub that can generate Nginx configuration with one click, which is quite awesome.

First, let’s take a look at the configuration of what functions it supports: reverse proxy, HTTPS, HTTP/2, IPv6, cache, WordPress, CDN, Node.js support, Python (Django) server, etc.

If you want to configure it online, you only need to open the website: https://nginxconfig.io/ and operate according to your needs.

Select your scenario, fill in the parameters, and the system will automatically generate a configuration file.

Open source address: github.com/digitalocean/nginxconfig.io

Website: digitalocean.com/community/tools/nginx

Recommended site searches: .cn domain name registration, US virtual space, purchase of domain name and space, site group server, Korean high-defense server, free international domain name, host sharing, Ministry of Industry and Information Technology website registration system, Shandong website registration, Filing system,

Nginx configuration code list: listening port, access logs, domain name, static assets, redirection, reverse proxy, load balancing and SSL protocol

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码