Condivisione della tecnologia

[Funzionamento e manutenzione della sicurezza di Linux] Relativo a Nginx

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Relativo a Nginx

1. Panoramica

Nginx è un server Web con elevata concorrenza e capacità di carico elevato e presenta i seguenti vantaggi:

  1. È stabile, consuma meno risorse di sistema e occupa meno memoria.
  2. Il pacchetto di installazione del software è piccolo e altamente personalizzabile.
  3. Ha un'elevata capacità di concorrenza e può gestire 30.000-50.000 richieste.

Come server web per pagine statiche, Nginx considera principalmente le sue prestazioni e presta grande attenzione all’efficienza.

File di configurazione (il file di configurazione più elementare):

Il file di configurazione è principalmente diviso in tre blocchi: blocco globale, blocco eventi e blocco http.

# 全局块
worker_processes  1;  
# 事件区块
events {
    worker_connections  1024;
}
# http区块
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost; #改为IP地址
        # 反向代理
        location / {
            root   html; #存放目录
            index  index.html index.htm;
        }
        # 错误页面路由
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

  • 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
  • 28

blocco globale: utilizzato per archiviare la configurazione complessiva del server Niginx, incluso il numero di processo correlato, l'ID del processo, il percorso di archiviazione, il percorso di archiviazione dei registri, il gruppo utenti e altre informazioni.

blocco eventi: utilizzato per memorizzare le configurazioni relative al server Niginx e alle connessioni utente.

blocco http: utilizzato per memorizzare alcuni controlli di accesso al server e configurazioni di terze parti, inclusi blocchi globali http e blocchi server.

2. Procura inversa

2.1 Relativo al proxy inverso

Quando si accede dal client, non è richiesta alcuna configurazione per accedere. Il server proxy inverso riceverà la richiesta del client, quindi selezionerà l'accesso e restituirà i dati ottenuti al client.Il vantaggio è che è possibile utilizzare un server proxy inversoNascondi l'IP del server reale ed esponi l'IP del server proxy

File di configurazione:

#接口端
location /police/ {
    proxy_pass   http://192.168.1.1:8852/police/;
    proxy_redirect default;
    proxy_http_version 1.1;
    proxy_connect_timeout   60;
    proxy_send_timeout      60;
    proxy_read_timeout      90;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Se incontri un'interfaccia che inizia con /police request, accedi a http://192.168.1.1:8852/police/.

Se si desidera definire un proxy inverso per più porte, è necessario modificare l'intestazione del proxy (posizione /police/) e l'indirizzo IP a cui si accede (proxy_pass http://192.168.1.1:8852/police/;).

2.2 Procura in avanti

Se gli utenti di una rete locale desiderano accedere a Internet e accedere al server tramite un server proxy, hanno bisogno di un server proxy di inoltro.

3. Bilanciamento del carico

Ottieni il bilanciamento del carico modificando le richieste dall'invio a un singolo server all'invio a più server.

#动态服务器组
upstream dynamic_zuoyu {
    server localhost:8080;  #tomcat 7.0
    server localhost:8081;  #tomcat 8.0
    server localhost:8082;  #tomcat 8.5
    server localhost:8083;  #tomcat 9.0
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Esistono quattro metodi di base per ottenere il bilanciamento del carico: metodo di polling, modalità peso peso, ip_hash, less_conn.

metodospiegare
metodo di pollingModalità di default
peso pesoDistribuire in base al peso
hash_ipassegnato in base all'ip
meno_connessiAssegnazione basata sul tempo minimo di connessione

3.1 Metodo di attuazione

  1. Metodo di polling (predefinito):

    Ogni richiesta viene assegnata a ciascun server una per una in ordine cronologico.

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
        	# server 参数
        	#fail_timeout 最大失败时间
        	#max_fails	设置在fail_timeout参数设置的时间内最大失败次数,超过则认为停机
        	#fail_time	服务器会被认为停机的时间长度,默认为10s
        	#backup	标记该服务器为备用服务器,当主服务器停止时,请求会被发送到它这里
        	#down	标记服务器永久停机
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  2. peso peso (sondaggio ponderato):

    Specificare la probabilità di polling e controllare l'accesso tramite il peso del peso. Il rapporto di accesso è proporzionale al peso.

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082   backup;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  3. hash_ip:

    La richiesta viene elaborata tramite un algoritmo hash. Quando l'utente accede nuovamente a un server, verrà automaticamente localizzato in base al risultato hash dell'IP di accesso, consentendo a ciascun utente di avere accesso fisso a un server back-end .

    #动态服务器组
    upstream dynamic_zuoyu {
        ip_hash;    #保证每个访客固定访问一个后端服务器
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  4. minima_connessione:

    Inoltra la richiesta al server backend con meno connessioni.

    #动态服务器组
    upstream dynamic_zuoyu {
        least_conn;    #把请求转发给连接数较少的后端服务器
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082 backup;  #tomcat 8.5
        server localhost:8083   max_fails=3	fail_timeout=20s;  #tomcat 9.0
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

4. Separazione del movimento e della statica

Le pagine dinamiche e le pagine statiche vengono analizzate da server diversi.

File di configurazione:

#访问静态资源服务器
location /image/ {
  root   /var/filecenter/;
}
location /static/ {
  root   /var/filecenter/;
}
location /car/ {
  root   /var/filecenter/;
}
location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
  root /Users/dalaoyang/Downloads/static;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
#动态页面访问后台服务
#接口端
        location /police/ {
            proxy_pass   http://192.168.1.1:8852/police/;
            proxy_redirect default;
            proxy_http_version 1.1;
            proxy_connect_timeout   60;
            proxy_send_timeout      60;
            proxy_read_timeout      90;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5. Comandi comuni

yum install nginx	安装nginx
netstat -anput|grep nginx 查看nginx进程
netstat -nltp	查看服务器端口占用情况
cd /usr/local/nginx/sbin/
./nginx  启动
./nginx -s stop  停止
./nginx -s quit  安全退出
./nginx -s reload  重新加载配置文件  如果我们修改了配置文件,就需要重新加载。
ps aux|grep nginx  查看nginx进程
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6. File di configurazione completo


#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 {
    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;

    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;
        }

        # 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;
    #    }
    #}

}


  • 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
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119