Nginx

2021-12-20 大约 2 分钟

# Nginx

  • master进程和worker进程

  • work工作原理:work process最好设置成cpu数

  • worker_connection:

  • HTTP服务器

     # worker对应进程的用户名,可设置nginx,nobody。master默认root
     user nginx;
    
     http {
         # 每个server块就相当于一个虚拟主机
         server {
             listen       80;
             server_name  localhost;
             client_max_body_size 1024M;
    
             location / {
                 # http://localhost/ 指向/usr/local/var/www/index.html
                 # http://localhost/test.html  指向/usr/local/var/www/html/test.html
                 # / 匹配到/test.html 从而使root + /test.html = /usr/local/var/www/html/test.html
                 root   /usr/local/var/www/html;
                 index  index.html index.htm;
             }
         }
     }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
  • 静态服务器

    http {
        server {
            listen       80;
            server_name  localhost;
    		
            # 设置变量
            set $doc_root /usr/local/var/www;
    		# = 精确匹配
            # ^~ 前缀匹配。如果匹配成功,则不再匹配其他location
            # ~ 正则匹配,区分大小写
            # ~* 正则匹配,不区分大小写
            # / 通用匹配
            
            # 访问http://localhost/images/test.jpg会映射到/usr/local/var/www/images/test.jpg
            # ^~ /images/ 匹配到root+/images/test.jpg
            location ^~ /images/ {
                root $doc_root;
           }
    	   # http://localhost/test.jpg会映射到/usr/local/var/www/images/test.jpg
           location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
               root $doc_root/img;
           }
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
  • 反向代理

    server {
        listen       80;
        server_name  localhost;
    
        location / {
            proxy_pass http://localhost:8081;
            proxy_set_header Host $host:$server_port;
            # 设置用户ip地址
            proxy_set_header X-Forwarded-For $remote_addr;
            # 当请求服务器出错去寻找其他服务器
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; 
        }
    
    }  
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
  • 负载均衡

    upstream web_servers {
        # 默认轮询
        server localhost:8081;  
        server localhost:8082; 
        # 权重
        server localhost:8081 weight=1;
        server localhost:8082 weight=3;
        server localhost:8083 weight=4 backup;  
        # ip_hash 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
        ip_hash;
        server localhost:8080;
        server localhost:8081;
    }
    
    server {
        listen       80;
        server_name  localhost;
        #access_log  logs/host.access.log  main;
    
    
        location / {
            proxy_pass http://web_servers;
            # 必须指定Header Host
            proxy_set_header Host $host:$server_port;
        }
     }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
  • 动静分离

    upstream web_servers {  
         server localhost:8081;  
         server localhost:8082;  
    }
    
    server {
        listen       80;
        server_name  localhost;
    
        set $doc_root /usr/local/var/www;
    
        location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
           root $doc_root/img;
        }
    
        location / {
            proxy_pass http://web_servers;
            # 必须指定Header Host
            proxy_set_header Host $host:$server_port;
        }
    
        error_page 500 502 503 504  /50x.html;  
        location = /50x.html {  
            root $doc_root;
        }
    
     }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
上次编辑于: 2021年12月20日 15:58