Home > Backend Development > PHP Tutorial > Nginx scenario application technology sharing

Nginx scenario application technology sharing

小云云
Release: 2023-03-19 17:58:02
Original
1452 people have browsed it

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>
Copy after login
Copy after login
Copy after login
Copy after login
<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>
Copy after login

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>
Copy after login
Copy after login
Copy after login
Copy after login
<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>
Copy after login

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>
Copy after login
Copy after login
Copy after login
Copy after login
<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>
Copy after login
<span style="font-size: 14px;"># 其他代理配置独立出去,方便复用<br>vi /etc/nginx/proxy_param<br></span>
Copy after login
<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>
Copy after login

Nginx as a load balancing service

Reference configuration

<span style="font-size: 14px;">vi /etc/nginx/conf.d/default.conf<br></span>
Copy after login
Copy after login
Copy after login
Copy after login
<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>
Copy after login

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>
Copy after login
<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>
Copy after login
<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>
Copy after login
<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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template