current position:Home>Nginx learning notes

Nginx learning notes

2022-04-29 16:14:51Study diary

Nginx note


Go directly to the official website to download :http://nginx.org/en/download.html

We choose a stable version  Insert picture description here

I only in D Disk creates a new directory and directly puts it in , And the directory name should not be in Chinese , Will report a mistake , Unzip to use ; Insert picture description here

double-click , You can start nginx Insert picture description here

Test access , You can see that it's open nginx Successful launch , Of course, the default boot port is 80 port , If you occupy or want to modify the port , Can be directly in conf
 Insert picture description here

Modify the port  Insert picture description here

 Insert picture description here

nginx stop it :

D:\Nignx\nginx-1.20.2>nginx.exe -s quit  # stop it nginx  Orderly stop 
D:\Nignx\nginx-1.20.2>nginx.exe -s stop

# notes :stop It's a quick stop nginx, It may not hold relevant information ;quit It's a complete and orderly stop nginx, And keep relevant information .

Nginx Common commands

Windows Next Nginx Start of 、 Stop wait command

stay Windows Next use Nginx, We need to master some basic operation commands , such as : start-up 、 stop it Nginx service , Reload the Nginx etc. , Now I'll give you a brief introduction .

1、 start-up :

C:\server\nginx-1.0.2>start nginx or 
C:\server\nginx-1.0.2>nginx.exe

2、 stop it :

C:\server\nginx-1.0.2>nginx.exe -s stop or 
C:\server\nginx-1.0.2>nginx.exe -s quit

#  notes :stop It's a quick stop nginx, It may not hold relevant information ;quit It's a complete and orderly stop nginx, And keep relevant information .

3、 Reload the Nginx:

C:\server\nginx-1.0.2>nginx.exe -s reload

When the configuration information is modified , Use this command when you need to reload these configurations .

4、 Reopen the log file :

C:\server\nginx-1.0.2>nginx.exe -s reopen

5、 see Nginx edition :

C:\server\nginx-1.0.2>nginx -v

6、 liunx see nginx process

ps aux | grep nginx

Nginx Quick to use

First of all, we can search the complete set of parameters , We can refer to the rookie tutorial :https://www.runoob.com/w3cnote/nginx-setup-intro.html

It is divided into the following modules :

  • 1、 Global block : Configuration impact nginx Global instructions . Generally there is operation nginx The user group for the server ,nginx process pid Storage path , Log storage path , Profile import , Allow to generate worker process Number, etc .
  • 2、events block : Configuration impact nginx Server or network connection to user . There is a maximum number of connections per process , Select which event-driven model handles connection requests , Allow multiple network connections at the same time , Open multiple network connection serialization, etc .
  • 3、http block : You can nest multiple server, Configure agent , cache , Configuration of most functions such as log definition and third-party modules . Such as file introduction ,mime-type Definition , Log customization , Whether to use sendfile Transfer files , Connection timeout , Number of single connection requests, etc .
  • 4、server block : Configure the parameters for the virtual host , One http There can be more than one server.
  • 5、location block : Configure the route of the request , And the processing of various pages .
**************************************************************************
== Global block ==
#user  nobody;
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 block ==

events {
    
    worker_connections  1024;
}
**************************************************************************


**************************************************************************

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



    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

==server block ==
    

    server {
    
        listen       80;       		# server Listening port 
        server_name  localhost;		# 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

    }

**************************************************************************

}


 Specific parameters can refer to :
###########  Each instruction must have a semicolon end .#################
#user administrator administrators;  # Configure users or groups , The default is nobody nobody.
#worker_processes 2;  # Number of processes allowed to generate , The default is 1
#pid /nginx/pid/nginx.pid;   # Appoint nginx Process running file storage address 
error_log log/error.log debug;  # Make a log path , Level . This setting can be put into a global block ,http block ,server block , This is the level :debug|info|notice|warn|error|crit|alert|emerg
events {
    
    accept_mutex on;   # Set up network connection serialization , To prevent the occurrence of shock , The default is on
    multi_accept on;  # Set whether a process accepts multiple network connections at the same time , The default is off
    #use epoll;      # Event driven model ,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    # maximum connection , The default is 512
}
http {
    
    include       mime.types;   # File extension and file type mapping table 
    default_type  application/octet-stream; # Default file type , The default is text/plain
    #access_log off; # Cancel service log     
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; # Custom format 
    access_log log/access.log myFormat;  #combined Default value for log format 
    sendfile on;   # allow sendfile Transfer files by , The default is off, Can be in http block ,server block ,location block .
    sendfile_max_chunk 100k;  # The number of transfers per process call cannot be greater than the set value , The default is 0, There is no upper limit .
    keepalive_timeout 65;  # Connection timeout , The default is 75s, Can be in http,server,location block .

    upstream mysvr {
       
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  # Hot standby 
    }
    error_page 404 https://www.baidu.com; # Error page 
    server {
    
        keepalive_requests 120; # Maximum number of single connection requests .
        listen       4545;   # Listening port 
        server_name  127.0.0.1;   # Monitor address        
        location  ~*^.+$ {
           # Requested url Filter , Regular matching ,~ For case sensitivity ,~* For case insensitive .
           #root path;  # root directory 
           #index vv.txt;  # Set default page 
           proxy_pass  http://mysvr;  # Request turn mysvr  List of defined servers 
           deny 127.0.0.1;  # refuse ip
           allow 172.18.5.54; # Allow the ip           
        } 
    }
}

Next, let's implement a simple use : Let's assume that we've used nginx Load balancing , We can add such a setting , Load balancing can be realized .

upstream mysvr {
       
    server 127.0.0.1:7878;  weight=1      #  Suppose it's your host A
    server 192.168.10.121:3333; weight=1  #  Your host B 
    # weight  The parameter is weight 
}

# stay server Add proxy parameters to 

location / {
    
    root   html;
    index  index.html index.htm;
	proxy_pass http://mysvr;   # visit http://mysvr  Can 
}

copyright notice
author[Study diary],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291426421429.html

Random recommended