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!

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
