技術共有

【Linuxセキュリティ運用・保守】Nginx関連

2024-07-12

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

Nginx関連

1。概要

Nginx は、高い同時実行性と高負荷機能を備えた Web サーバーであり、次の利点があります。

  1. 安定しており、システム リソースの消費とメモリの使用量が少なくなります。
  2. ソフトウェア インストール パッケージは小さく、高度にカスタマイズ可能です。
  3. 高い同時実行機能を備えており、30,000 ~ 50,000 のリクエストを処理できます。

静的ページの Web サーバーとして、Nginx は主にパフォーマンスを考慮し、効率に細心の注意を払っています。

設定ファイル (最も基本的な設定ファイル):

設定ファイルは主に、グローバル ブロック、イベント ブロック、http ブロックの 3 つのブロックに分かれています。

# 全局块
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

グローバルブロック: 関連するプロセス番号、プロセス ID、ストレージ パス、ログ ストレージ パス、ユーザー グループ、その他の情報を含む、niginx サーバーの全体的な構成を保存するために使用されます。

イベントブロック: niginx サーバーおよびユーザー接続に関連する構成を保存するために使用されます。

httpブロック: http グローバル ブロックやサーバー ブロックなど、一部のサーバー アクセス制御およびサードパーティ構成を保存するために使用されます。

2. リバースプロキシ

2.1 リバースプロキシ関連

クライアントからアクセスする場合、アクセスするための設定は必要ありません。リバースプロキシサーバーはクライアントのリクエストを受信し、リバースプロキシサーバーがアクセスを選択して取得したデータをクライアントに返します。この利点は、リバース プロキシ サーバーを使用すると、実サーバー IP を非表示にし、プロキシ サーバー IP を公開します。

設定ファイル:

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

/police リクエストで始まるインターフェイスが見つかった場合は、http://192.168.1.1:8852/police/ にアクセスします。

複数のポートにリバース プロキシを定義する場合は、プロキシ ヘッダー (場所 /police/) とアクセスされる IP アドレス (proxy_pass http://192.168.1.1:8852/police/;) を変更する必要があります。

2.2 フォワードプロキシ

ローカル エリア ネットワーク内のユーザーがインターネットにアクセスし、プロキシ サーバー経由でサーバーにアクセスする場合は、フォワード プロキシ サーバーが必要です。

3. 負荷分散

リクエストの送信を単一のサーバーから複数のサーバーに変更することで、負荷分散を実現します。

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

負荷分散を実現するには、ポーリング方式、重み付けモード、ip_hash、least_conn という 4 つの基本的な方法があります。

方法説明する
ポーリング方法デフォルトモード
体重重量に応じて分配する
ip_ハッシュIPに基づいて割り当てられます
最小接続最小接続時間に基づく割り当て

3.1 実装方法

  1. ポーリング方法 (デフォルト):

    各リクエストは時系列に 1 つずつ各サーバーに割り当てられます。

    #动态服务器组
    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. 重み 重み (加重ポーリング):

    ポーリング確率を指定し、重みの重みによってアクセスを制御します。アクセス率は重みに比例します。

    #动态服务器组
    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. ip_ハッシュ:

    リクエストはハッシュ アルゴリズムを通じて処理され、ユーザーが再度サーバーにアクセスすると、アクセス IP のハッシュ結果に従ってリクエストが自動的に割り当てられるため、各ユーザーはバックエンド サーバーに固定的にアクセスできます。 。

    #动态服务器组
    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. 最小接続数:

    接続数が少ないバックエンド サーバーにリクエストを転送します。

    #动态服务器组
    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. 動と静の分離

動的ページと静的ページは異なるサーバーによって解析されます。

設定ファイル:

#访问静态资源服务器
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. 共通コマンド

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. 完全な設定ファイル


#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