Berbagi teknologi

[Operasi dan pemeliharaan keamanan Linux] Terkait Nginx

2024-07-12

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

Terkait Nginx

1 Ikhtisar

Nginx adalah server web dengan konkurensi tinggi dan kemampuan beban tinggi, serta memiliki keunggulan sebagai berikut:

  1. Ini stabil, menggunakan lebih sedikit sumber daya sistem, dan menggunakan lebih sedikit memori.
  2. Paket instalasi perangkat lunak berukuran kecil dan sangat dapat disesuaikan.
  3. Ini memiliki kemampuan konkurensi tinggi dan dapat menangani 30.000-50.000 permintaan.

Sebagai server web untuk halaman statis, Nginx terutama mempertimbangkan kinerjanya dan sangat memperhatikan efisiensi.

File konfigurasi (file konfigurasi paling dasar):

File konfigurasi terutama dibagi menjadi tiga blok: blok global, blok peristiwa, dan blok 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

blok global: Digunakan untuk menyimpan keseluruhan konfigurasi server niginx, termasuk nomor proses terkait, ID proses, jalur penyimpanan, jalur penyimpanan log, grup pengguna, dan informasi lainnya.

blok acara: Digunakan untuk menyimpan konfigurasi yang terkait dengan server niginx dan koneksi pengguna.

blok http: Digunakan untuk menyimpan beberapa kontrol akses server dan konfigurasi pihak ketiga, termasuk blok global http dan blok server.

2. Membalikkan proksi

2.1 Terkait proksi terbalik

Saat diakses oleh klien, tidak diperlukan konfigurasi untuk mengakses. Server proxy terbalik akan menerima permintaan klien, dan kemudian server proxy terbalik akan memilih akses dan mengembalikan data yang diperoleh ke klien.Keuntungannya adalah menggunakan server proxy terbalik bisaSembunyikan IP server sebenarnya dan tampilkan IP server proxy

Berkas konfigurasi:

#接口端
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

Jika Anda menemukan antarmuka yang dimulai dengan permintaan /police, akses http://192.168.1.1:8852/police/.

Jika Anda ingin menentukan proxy terbalik untuk beberapa port, Anda perlu mengubah header proxy (lokasi /police/) dan alamat IP yang diakses (proxy_pass http://192.168.1.1:8852/police/;).

2.2 Meneruskan proksi

Jika pengguna di jaringan area lokal ingin mengakses Internet dan mengakses server melalui server proxy, mereka memerlukan server proxy penerusan.

3. Penyeimbangan beban

Capai penyeimbangan beban dengan mengubah permintaan dari dikirim ke satu server menjadi dikirim ke beberapa 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

Ada empat metode dasar untuk mencapai penyeimbangan beban: metode polling, mode bobot bobot, ip_hash, paling sedikit_conn.

metodemenjelaskan
metode pemungutan suaraModus bawaan
berat badanBagikan sesuai beratnya
hash_ipditugaskan berdasarkan ip
paling sedikit_connAlokasi berdasarkan waktu koneksi minimum

3.1 Metode pelaksanaan

  1. Metode pemungutan suara (default):

    Setiap permintaan ditugaskan ke setiap server satu per satu dalam urutan kronologis.

    #动态服务器组
    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. bobot bobot (polling tertimbang):

    Tentukan probabilitas polling, dan kendalikan akses melalui bobot bobot.

    #动态服务器组
    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:

    Permintaan diproses melalui algoritma hash. Ketika pengguna mengakses server lagi, permintaan tersebut akan ditempatkan secara otomatis. Setiap permintaan dialokasikan sesuai dengan hasil hash dari IP akses, sehingga setiap pengguna memiliki akses tetap ke 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. paling sedikit_koneksi:

    Meneruskan permintaan ke server backend dengan koneksi lebih sedikit.

    #动态服务器组
    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. Pemisahan gerak dan statis

Halaman dinamis dan halaman statis diurai oleh server yang berbeda.

Berkas konfigurasi:

#访问静态资源服务器
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. Perintah umum

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 konfigurasi lengkap


#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