Nginx Construction and Use

Posted by mjdamato on Sat, 18 May 2019 17:24:19 +0200

I. Introduction

Nginx (engine x) is a high performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP server. [Introduction to Baidu Encyclopedia] Reverse Delivery and Load Balancing are commonly used

Home brew Installation

Installation of Nginx, here we use the homebrew tool to install the diagram. As for what homebrew can do, the official website should explain that installing Apple with Homebrew is not pre-installed but what you need. homebrew official website
Here is the installation and uninstallation commands, as for the specific details of the tutorial can Baidu, this installation is very simple.
Installation command: / usr/bin/ruby -e "$(curl -fsSL) https://raw.githubusercontent.com/Homebrew/install/master/install)"
Uninstall command: ruby-e "$(curl-fsSL) https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

III. Nginx Installation

Installation details can be viewed Blog about Dream Search 1314

4. Common Nginx commands

Start: nginx

Restart: nginx-s reload

Stop: nginx-s QUST [with log] or nginx-s stop [without log]

5. Reverse proxy configuration

Modify / usr/local/etc/nginx/nginx.conf to configure the address of the agent in nginx.conf. The syntax is as follows:

server{
    listen [The port number you want to monitor];
    server_name [The domain name you want to monitor/IP];
    location / {
        proxy_pass [The target address of the agent];
     }
}

Examples are as follows:

server{
        listen 8090;
        server_name 127.0.0.1;
        location / {
               proxy_pass httP://127.0.0.1:8081;
        }
    }

It is important to note that a successful configuration requires a restart of nginx to take effect. The local 8081 access address is proxied here. After the project of the local 8081 port is started, it can be accessed by 127.0.0.1:8090/XXX.

VI. Load Balancing Configuration

html {
    ..........
    # load balancing
    upstream loadBalancing {
        server 192.168.0.112:8081 weight=1;
        server 127.0.0.1:8081 weight=2;
    }
    server{
        listen 8085;
        server_name  localhost;
        location / {
            proxy_pass http://loadBalancing;
        }
    }
}

When the weight value is encountered, the higher the weight is, the higher the probability of distribution is. After the project starts, when local host: 8085/XXX is accessed locally, nginx is distributed to tomcat on two servers in load Balancing

VII. Other Contents

1. Introduction to naginx.conf

1.1 Source code is as follows

#Define users and user groups for Nginx running to specify users and user groups for the Nginx Worker process to run and run by default from the nobody account
#user  nobody;
#The number of nginx processes is recommended to be equal to the total number of CPU cores.
worker_processes  1;
#Global error log definition type, [debug | info | notice | warn| error | crit ],among debug The output log is the most detailed, and crit Minimum output log
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#Process file, which specifies the location of the storage file for the process id
#pid        logs/nginx.pid;

#Working Mode and Connection Number Upper Limit
events {
    #Limited by the maximum number of open files in the Linux system process, the worker_connections settings will not take effect until the operating system command "ulimit-n 65536" is executed.
    worker_connections  1024;
}

#Setting up http server
http {
    #To tell nginx to identify the file type by using the MIME type of the settings file, the mime.type file definition of the type in the configuration file directory.
    include       mime.types;
    default_type  application/octet-stream;#Default file type
    #Used to format the log and record which parameters, set here to main, just for access_log to record this type
    #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;
    #Open efficient file transfer mode. Sendfile instruction specifies whether nginx calls sendfile function to output files. For common applications, it is on. If it is used for downloading and other application disk IO overload applications, it can be set off to balance disk and network I/O processing speed and reduce system load. Note: If the picture does not display properly, change this to off.
    sendfile        on;
    #tcp_nopush     on;#Preventing network congestion

    #keepalive_timeout  0;
    keepalive_timeout  65;#Long connection timeout in seconds

    #gzip  on;#Turn on gzip compressed output
    #Configuration of Virtual Host
    server {
        listen       8080;#Monitor port
        server_name  localhost;#Domain names can be multiple, separated by spaces

        #charset koi8-r;#Default encoding

        #access_log  logs/host.access.log  main;
        #Represents all root web root directories within the entire server virtual host. Note that root/data/www/*** should be distinguished from the definition below locate {};
        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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # 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 servers/*;
    server{
        listen 8090;
        server_name 127.0.0.1;
        location / {
               proxy_pass httP://127.0.0.1:8081;
        }
    }
}

May refer to Blog of aLOCK1 and nginx configuration, virtual host, load balancing and reverse proxy Introduction

Topics: Nginx PHP Ruby curl