nginx configuration multisite split profile

1. Background

There are multiple sites configured with the same nginx, the configuration files are disassembled for subsequent maintenance, and one site modifies the backup without affecting other sites.

Nginx configuration directory: /usr/local/nginx/conf

2. Create a new directory

Create new directories conf.d and upstream.d

Directory where conf.d stores front-end configuration

upstream.d Directory to store backend site configuration

3. nginx.conf

The main profile configures only the parameters of the nginx program itself.

Loading front-end and back-end configurations through include in the http module of the main configuration file.

#user  nobody;
user root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    client_max_body_size 15m; #Maximum number of single file bytes allowed for client requests
    client_body_buffer_size 256k; #Buffer Agent Buffers Maximum Bytes of Client Requests

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include /usr/local/nginx/conf/upstream.d/*.conf; # Front End Configuration
    include /usr/local/nginx/conf/conf.d/*.conf; # Backend Configuration
}

Global block: Configure instructions that affect the nginx global. There are usually user groups running nginx servers, pid storage paths for nginx processes, log storage paths, profile introduction, allowed generation of worker process es, and so on.

Evets block: Configuration affects the nginx server or network connection to the user. There is a maximum number of connections per process, which event-driven model is chosen to handle connection requests, whether multiple network connections are allowed to be accepted at the same time, multiple network connection serialization is turned on, and so on.

http block: can nest multiple server s, configure proxy, cache, log definition, and most other features and configuration of third-party modules. Such as file introduction, mime-type definition, log customization, whether to use sendfile to transfer files, connection timeout, number of single connection requests, etc.

Reference resources: https://www.cnblogs.com/duhuo/p/5567177.html

4. Front End Configuration

Enter directory conf.d

New file web1.conf

    server {
        listen       9004; #This line indicates that the server block listens on port 80 and handles requests whenever a request accesses port 80
        server_name  localhost; #  The name of the virtual host that this server block represents
        access_log /var/log/nginx/web1-access.log;
		
    	# Indicates that if the request path is/is processed with this location block
        location / {
            root  /data/web/web1-vue; # Root directory, front-end file path
            #You need to point to @router below or the route where vue appears refreshes in nginx with 404
            try_files $uri $uri/ @router;
            index  index.html index.htm; #Set Default Page
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
                #Corresponding to @router above, the main reason is that the route resource of the route is not a real path, so no specific file can be found
        #So rewrite to index is required. In HTML and then handed over to routing in processing request resources
        location @router {
            rewrite ^.*$ /index.html last;
        }
			   #Request url filtering, regular matching, ~case sensitive, ~*case insensitive
                # Path matching, proxying to devServer2 when matched to ^~/p-api
                # ^~/p-api: Do not test the regular expression if the path matches
                location ^~/p-api {
                   proxy_set_header Host $host;
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_buffering off;
                   rewrite ^/prod-api/(.*)$ /$1 break; # With the break directive, rewrite does not jump out of the location scope, and its life ends in this location
                   proxy_pass http://devServer2; # Request to go to the list of servers defined by mdevServer2
                }

                location ^~/cWeb {
                   proxy_set_header Host $host;
                   proxy_set_header X-Real-IP $remote_addr;
                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                   proxy_buffering off;
                   rewrite ^/configWeb/(.*)$ /$1 break;
                   proxy_pass http://gateway2;
                }

    }

server global block: The most common configuration is the listening configuration of this virtual machine host and the name or IP configuration of this virtual machine host

location block: https://www.cnblogs.com/duhuo/p/8323812.html

location syntax

location [=|~|~*|^~] /uri/ { ... }
=         Strict match. If the request matches this location´╝îThe search will be stopped and the request will be processed immediately
~         Case sensitive matching(Available Regular Expressions)
~*       Case insensitive matching(Available Regular Expressions)
!~       Case-sensitive mismatch
!~*     Case insensitive mismatch
^~      If you use this prefix for a regular string,Then tell nginx Do not test the regular expression if the path matches
  • The name of the split file is custom, nginx reads only what's inside it

5. Backend Site Configuration

Enter the directory upstream.d

New file devServer2.conf, proxy_pass configuration will use

upstream devServer2 {
   server 172.16.2.100:20000; #Background interface
}
  • The name of the split file is custom, nginx reads only what's inside it

Tags: Front-end

Posted by fabby on Fri, 06 May 2022 19:01:52 +0300