Nginx scenario application technology sharing
This article mainly shares with you Nginx scene application technology, hoping to help everyone.
Nginx as a static resource web service
Configuration context: http, server or location.
May involve scenarios such as resource compression, cross-domain access, and anti-leeching.
Context of resource compression configuration: http, server or location
Context of cross-domain configuration: http, server or location
Context of anti-leeching configuration: server, location
Configuration reference
<span style="font-size: 14px;">vi /etc/nginx/conf.d/default.conf<br></span>
<span style="font-size: 14px;">server {<br> ...<br> <br> # 开启sendfile,提高网络包的传输效率<br> sendfile on;<br> <br> # 配置图片资源的存放路径及压缩方式<br> location ~ .*\.(jpg|gif|png)$ {<br> gzip on;<br> gzip_http_version 1.1;<br> gzip_comp_level 2;<br> gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;<br> # 图片防盗链配置 <br> # 不指定referer 协议不正确 放行指定IP 放行SEO优化<br> valid_referers none blocked 39.104.116.91 ~/google\./;<br> if ($invalid_referer) {<br> return 403;<br> }<br> root /opt/app/code/images;<br> }<br><br> # 配置txt|xml资源的存放路径及压缩方式<br> location ~ .*\.(txt|xml)$ {<br> gzip on;<br> gzip_http_version 1.1;<br> gzip_comp_level 1;<br> gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;<br> root /opt/app/code/doc;<br> }<br> # 缓存与跨域场景配置<br> location ~ .*\.(html|htm)$ {<br> #给返回报文添加Cache-Control、Expires头,控制缓存<br> #expires 24h;<br> #允许跨域到指定域<br> add_header Access-Control-Allow-Origin http://somehost.com;<br> add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;<br> root /opt/app/code;<br> }<br><br>}<br></span>
Nginx as proxy service
Forward Proxy
Forward proxy is the client proxy. The client sets up a proxy server to meet the access requirements for the target address.
Nginx can act as this proxy server
Configuration reference
<span style="font-size: 14px;">vi /etc/nginx/conf.d/default.conf<br></span>
<span style="font-size: 14px;">server {<br> ...<br> # 可能会涉及到DNS域名解析<br> resolver 8.8.8.8;<br> location / {<br> # 原封不动的让自己转发客户端的请求<br> proxy_pass http://$http_host$request_uri;<br> }<br>}<br></span>
Reverse proxy
Reverse proxy is a server proxy. Hide the real server address from clients.
Configuration Reference
<span style="font-size: 14px;">vi /etc/nginx/conf.d/default.conf<br></span>
<span style="font-size: 14px;">server {<br>location / {<br> #Real Server地址<br> proxy_pass http://127.0.0.1:8080;<br> include proxy_params;<br> }<br>}<br></span>
<span style="font-size: 14px;"># 其他代理配置独立出去,方便复用<br>vi /etc/nginx/proxy_param<br></span>
<span style="font-size: 14px;"># default就可以了。除非返回301的场景,可能需要改写<br>proxy_redirect defalut;<br><br>#配置header信息,让Real Server了解实际客户端信息<br>proxy_set_header Host $http_host;<br>proxy_set_header X-Real-IP $remote_addr;<br><br>#一些代理超时设置<br>proxy_connect_timeout 30;<br>proxy_send_timeout 60;<br>proxy_read_timeout 60;<br><br>#代理缓冲区设置<br>proxy_buffer_size 32k;<br>proxy_buffering on;<br>proxy_buffers 4 128k;<br>proxy_busy_buffers_size 256k;<br>proxy_max_temp_file_size 256k;<br></span>
Nginx as a load balancing service
Reference configuration
<span style="font-size: 14px;">vi /etc/nginx/conf.d/default.conf<br></span>
<span style="font-size: 14px;">upstream backend {<br> server 192.168.1.101:8001;<br> server 192.168.1.102:8002;<br> server 192.168.1.102:8003 down;<br> server 192.168.1.103:8004 backup;<br>}<br>server {<br> ...<br> location / {<br> # 代理到upstream组<br> proxy_pass http://backend;<br> include proxy_params;<br> }<br>}<br></span>
server additional parameters
The server in the upstream group supports adding The following parameters:
Parameters | Description |
---|---|
#down | Do not participate in load balancing |
backup | Reserved backup server. It only provides service when no other node provides service |
max_fails | The number of allowed request failures |
fail_timeout | The time the service is suspended after max_fails fails |
max_conns | Limit the maximum number of connections received |
Load Balanced scheduling algorithm
Method | Description |
---|---|
Polling | Assigned to different backend servers one by one in sequence |
Weighted polling | The larger the weight value, the greater the probability of allocation |
ip_hash | The same IP has fixed access to the same back-end server |
least_conn | The machine with the lowest number of connections will be sent to the machine |
url_hash | According to the hash result of the url parameter Assign |
hash key value | hash custom key |
<span style="font-size: 14px;"># 加权轮询<br>upstream backend {<br> server 192.168.1.101:8001 weight=5;<br> server 192.168.1.102:8002;<br> server 192.168.1.102:8003 down;<br> server 192.168.1.103:8004 backup;<br>}<br></span>
<span style="font-size: 14px;"># IP HASH<br>upstream backend {<br> ip_hash;<br> server 192.168.1.101:8001;<br> server 192.168.1.102:8002;<br> server 192.168.1.102:8003 down;<br> server 192.168.1.103:8004 backup;<br>}<br></span>
<span style="font-size: 14px;"># 最少连接数<br>upstream backend {<br> least_conn;<br> server 192.168.1.101:8001;<br> server 192.168.1.102:8002;<br> server 192.168.1.102:8003 down;<br> server 192.168.1.103:8004 backup;<br>}<br></span>
<span style="font-size: 14px;"># URL HASH<br>upstream backend {<br> url_hash;<br> server 192.168.1.101:8001;<br> server 192.168.1.102:8002;<br> server 192.168.1.102:8003 down;<br> server 192.168.1.103:8004 backup;<br><br></span>
Related recommendations:
nginx speed limit connection number limit detailed explanation
nginx configuration React static page example tutorial
How to implement reverse proxy using Nginx in php
The above is the detailed content of Nginx scenario application technology sharing. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".
