Home  >  Article  >  Backend Development  >  Nginx server configuration instructions

Nginx server configuration instructions

WBOY
WBOYOriginal
2016-07-30 13:30:091289browse

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