search
HomeBackend DevelopmentPHP TutorialNginx server configuration instructions

Nginx server configuration instructions:
Rewrite function, proxy function

Rewrite function

Configuration instructions of the back-end server group

upstream instruction

upstream instruction is the main instruction to set up the back-end server group

<code>upstream name {<span>...</span>} </code>

Requests are scheduled according to round-robin (RR) Policy order selection server processing

server

server directive is used to set the server in the group

<code>server address [<span>params</span>];</code>
  • address: server address, which can include a port number or a Unix Domain Socket for inter-process communication prefixed with "unix:"
  • params: configure more properties for the current server.
    weight=number, the weight of the servers in the group, requests with higher weights will be processed first (weighted polling strategy is adopted)
    max_fails=number, sets the number of failed requests. When the number of failed requests to a server in the group exceeds this variable, the server is considered invalid (except 404)
    fail_timeout=time, set the time to try to request a server in a group and check whether the server is valid
    backup, mark the server as a backup server
    down, marking the server as permanently invalid

ip_hash command

ip_hash command is used to implement the session persistence function, directing multiple requests from a client to the same server in the group to ensure a stable session between the client and the server .
Note: The ip_hash command cannot be used with the weight variable. In the entire system, the Nginx server must be the front-end server, and the client address must be a Class C address.

keepalive command

keepalive command is used to control network connection maintenance Function

<code>keepalive connections;</code>

Set the upper limit of the number of idle network connections that each worker process of the server allows the server group to maintain

least_conn directive

least_conn directive is used to configure the Nginx server to use a load balancing strategy to allocate network connections within the server group Server, allocate requests to the server with the least current network connection

Configuration instructions for the Rewrite function

Multiple applications of the Rewrite function

Proxy function

Nginx forward proxy service configuration instructions

resolver instructions

resolver instructions are used Specify the IP address of the DNS server

<code>resolver address <span>...</span> [valid=time];</code>
  • address, the IP address of the DNS server, the default port is 35
  • time, set the validity time of the data packet in the network

resolve_timeout command

resolve_timeout command is used to set the DNS server domain name resolution Timeout time

<code>resolve_timeout <span>time</span>;</code>

proxy_pass command

proxy_pass command is used to set the protocol and address of the proxy server

<code>proxy_pass <span>URL</span>;</code>

Nginx reverse proxy service configuration command

proxy_pass command

proxy_pass command is used to set the address of the proxy server, which can be the host Name, IP address plus port number, etc.

<code>proxy_pass <span>URL</span>;</code>

proxy_hide_header command

proxy_hide_header command is used to set the Nginx server to hide some header field information when sending HTTP responses

<code>proxy<span>\_</span>hide_header field</code>

proxy_pass_header command

proxy_pass_header command is used to set those header field information is sent

<code>proxy<span>\_</span>hide_header field</code>

proxy_pass_header directive

proxy_pass_header directive is used to set which header field information is sent

<code>proxy<span>\_</span>hide_header field</code>

omitted

Nginx reverse proxy service - load balancing

load balancing of general polling rules

<code><span>...</span>
upstream backend {
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name www.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}</code>

weighted polling rules Load balancing

<code><span>...</span>
upstream backend {
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span> weight=<span>5</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span> weight=<span>2</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name www.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}</code>

Load balancing of specific resources

<code><span>...</span>
upstream videobackend {
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
upstream filebackend {
    server <span>192.168</span><span>.1</span><span>.5</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.6</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.7</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name www.mysite.name;
    index index.html index.htm;
    location /video/ {
        proxy_pass http://videobackend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    location /file/ {
        proxy_pass http://filebackend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        <span>...</span>
    }
    <span>...</span>
}</code>

Load balancing for different domain names

<code><span>...</span>
upstream bbsbackend{
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span> weight=<span>2</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span> weight=<span>2</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
upstream homebackend {
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.5</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.6</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name home.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://homebackend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}

server {
    listen <span>81</span>;
    server_name bbs.mysite.name;
    index index.html index.htm;
    location / {
        proxy_pass http://bbsbackend;
        proxy_set_header Host $host;
        <span>...</span>
    }
    <span>...</span>
}</code>

Load balancing with URL rewriting

<code><span>...</span>
upstream backend{
    server <span>192.168</span><span>.1</span><span>.2</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.3</span>:<span>80</span>;
    server <span>192.168</span><span>.1</span><span>.4</span>:<span>80</span>;
}
server {
    listen <span>80</span>;
    server_name www.mysite.name;
    index index.html index.htm;
    location /file/ {
        rewrite ^(/file/.*)/media/(.*)\.*$ $<span>1</span>/mp3/$<span>2.</span>mp3 last;
    }

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        <span>...</span>
    }
}</code>

The above introduces the Nginx server configuration instructions, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.