【Nginx】Nginxの設定ファイルについて詳しく解説、この記事でNginxの設定ファイルが理解できます

1.nginx

Nginx (エンジン x) は、高性能 HTTP およびリバース プロキシ Web サーバーであり、IMAP/POP3/SMTP サービスも提供します。

2. nginx設定ファイルのディレクトリ

  • fastcgi.conf: fastcgi 関連の設定を保存します
  • fastcgi.conf.default: 復元用の fastcgi.conf の元のバックアップ ファイル
  • fastcgi_params: fastcgi 関連のパラメータ ファイル
  • fastcgi_params.default: 復元に使用される fastcgi_params の元のバックアップ ファイル
  • koi-utf: コード変換マッピングファイル
  • koi-win: コード変換マッピングファイル
  • mime.types: メディア リソースのタイプを保存します。例: xml、html、css
  • mime.types.default: 復元に使用される mime.types の元のバックアップ ファイル
  • nginx.conf: nginx のデフォルトのメイン設定ファイル
  • nginx.conf.default: 復元用のnginx.confの元のバックアップファイル
  • scgi_params: fastcgi_params と同じ、どのサーバー変数が渡されるか
  • scgi_params.default: 復元に使用される scgi_params の元のバックアップ ファイル
  • uwsgi_params: サーバーとサーバー アプリケーション間の通信プロトコル。リクエストをアプリケーションに転送して返す方法を指定します。
  • uwsgi_params.default: 復元に使用される uwsgi_params の元のバックアップ ファイル

3. nginx設定ファイル(nginx.conf)の共通設定

nginx 設定ファイルは主に 6 つの領域に分かれています

  • main: グローバル設定 (スコープ グローバル)
  • イベント: nginx 作業モード
  • http: http 設定
    • アップストリーム: 負荷分散サーバーの設定
      • サーバー: ホスト設定 (サーバーノードは仮想マシンです)
        • 場所:URL設定

1. nginxコアモジュールの機能に対応するメインのメイン設定領域

#main区域,主配置区域,全局的

#定义 www 运行的用户和用户组,建议新建 www 用户和用户组 
user  www;

#启动工作进程数数量,一般设置和CPU核心数一致
worker_processes  1;    

#全局错误日志定义类型.【debug|info|notice|warm|error|crit】
error_log  logs/error.log;   
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程pid文件位置
#pid        logs/nginx.pid;    

3.2 イベントエリア

#events区域
events {
    
    
    #一个进程可以产生多少个工作的连接,nginx的最大并发访问量
    worker_connections  1024; 
    
    #使用epoll 模型,异步IO 处理模型,epoll 模型没有1024 的限制,并发发访问量特别快
    use epoll;    
}

3.3 nginxコアモジュールの共通設定パラメータ


http {
    
    
    #包含媒体资源类型的文件,调用mime.types文件.
    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;
    
    #作为web服务器的时候打开sendfile加快静态文件传输
    sendfile        on;
    
    #在开启了sendfile的情况下,合并请求后统一发送给客户端。
    tcp_nopush     on;    
    
    #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认On时,不延迟发送,立即发送用户相应报文。
    tcp_nodelay  off;
    
    #长连接超时时间,单位是秒
    keepalive_timeout  65;   
    
    #设置了每个散列桶占用的内存大小
    types_hash_max_size 2048;
    
    #开启压缩功能.
    gzip  on;
    
    #压缩的缓冲区大小
    gzip_buffers 16 8k;
    
	#压缩级别,默认为1
    gzip_comp_level 6;
    
	# 禁止那些浏览器压缩功能
	gzip_disable "MSIE [1-6].(?!.*SV1)";
   
	#小于128字节不压缩
	gzip_min_length 128;
    
    #只对 那些http版本进行压缩 默认 1.1 
	gzip_http_version 1.1;
    
 
    #用来配置虚拟主机,每一个server 段都是一个虚拟主机
    server {
    
    
        #监听80 端口,可以做基于端口的虚拟主机,listen 后面 IP加端口就是基于IP的虚拟主机
        listen       80; 
        
        #用来定义虚拟主机名称即域名; 支持*通配符,支持~起始字符做正则表达式匹配
        server_name  localhost;
        
		#设置编码格式,默认是俄语格式,可以改为utf-8
        charset utf-8;
        
 		#定义虚拟主机的访问日志
        access_log  /var/log/nginx/host.access.log  main;
 
        ###localtion 匹配段,用于匹配
        location / {
    
    
            #设置 WEB 资源路径映射,用于指明用户请求的 uri 所对应的本地文件系统上的文档目录.
            root   /usr/share/nginx/html;
            
            #默认资源设置
            index  index.html index.htm;
        }
          
 		#当客户端访问出错时定义返回的页面, 跳转404页面 /404.html;   
        #error_page  404              
        # 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 api scripts to Apache listening on xxx.xxx.xxx.xxx
        location /api/ {
    
    
            
            # webapi服务的IP地址和端口,内网地址
            proxy_pass         http://xxx.xxx.xxx.xxx;
            proxy_set_header   Host              $host;
            proxy_set_header   X-Real-IP         $remote_addr;
            proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   X-Forwarded-Port  $server_port;
            
			#此指令设置nginx能处理的最大请求主体大小
            client_max_body_size 200m;
            #接超时时间
            proxy_read_timeout  500;
        }

      
        # 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.conf;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
    
    
        #    deny  all;
        #}
    }
 

4. nginxフロントエンドの設定方法

静的ファイルホスティング用の html ファイルを nginx ディレクトリに追加します (ポート 8080)。

フロントエンドプロジェクトを/nginx/htmlフォルダーにコピーします。
nginx配下の設定ファイル(/nginx/conf/nginx.conf)内のサーバーノードの設定を、設定するドメイン名に追加します。

server {
    
    
        #监听的端口,
        listen       8080;
    
        #此处localhost可改为要访问的域名或者ip地址
        server_name  localhost;
    
        #编码
        charset utf-8;

        #access_log  logs/host.access.log  main;
    
        #error_page  404              /404.html;

        location / {
    
    
            #nginx下HTML文件夹,访问上述域名时会检索此文件夹下的文件进行访问
            root   html;
            #输入网址(server_name:port)后,默认的访问页面
            index  index.html index.htm;
        }
}

設定が正しくなったら、nginx を再起動します (サービス nginx の再起動)。

service nginx restart

テストアクセス

5. nginx プロキシ バックエンド API

クロスドメインの問題がある場合。プロキシ設定を追加できます (//xxx.xxx.xxx.xxx を /api に置き換えます)。

location /api/ {
    
    
    # webapi服务的IP地址和端口,内网地址
    proxy_pass         http://xxx.xxx.xxx.xxx;
    
    #重定义发往后端服务器的请求头
    proxy_set_header   Host              $host;
    proxy_set_header   X-Real-IP         $remote_addr;
    proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   X-Forwarded-Port  $server_port;

    client_max_body_size 200m;
    proxy_read_timeout  500;
}

6.nginx サービスステータスの表示

サーバーの実行ステータスを表示するには、Nginx Status を有効にするように Nginx を構成する必要があります

nginx ステータスを開く

server {
    
    
        #监听的端口,
        listen       8080;
        
        ···

        location /status {
    
    
            stub_status on;
            access_log off;            
        }
    
        ····
}

nginxを再起動するコマンドは比較的簡単なので、環境に応じてnginxを再起動してください。
ステータス ページを開き、ブラウザに nginx のアドレス xxx.xxx.xxx.xxx/status を入力して表示します...

Active connections: 4 
server accepts handled requests
 4 4 15 
Reading: 0 Writing: 1 Waiting: 3 

Active connections: 4 
表示Nginx正在处理的活动连接数4个。

server accepts handled requests
 4 4 15 
第一个server表示Nginx启动到现在共处理了4个连接
第二个accepts表示Nginx.启动到现在共成功创建4次握手
第三个handled requests表示总共处理了15次请求

Reading: 0 Writing: 1 Waiting: 3 
Reading:读取到客户端的Header信息数
Writing:返回给客户端Header信息数
Waiting:已经处理完正在等候下一次请求指令的驻留链接(开启keep-alive的情况下,这个值等于Active-(Reading+Writing)

7. nginx はログをどのように表示しますか

1.nginxのアクセスログを見る

  1. サーバーセクションまたはHTTPのaccess_logディレクティブを使用して、アクセスログを有効にします。

デフォルトでは、アクセス ログは Nginx 構成ファイルで定義されます。したがって、すべての仮想ホストのアクセス ログは同じ構成ファイルに保存されます。

http {
    
    
      ...
      #自定义访问日志中的格式
      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  /var/log/nginx/access.log;
      #关闭指令
      #access_log off
      ...
}

2. すべての仮想ホストのアクセス ログを、新しい別のファイルにログインして分離することをお勧めします。

http {
    
    
      ...
      ...
      #自定义访问日志中的格式
      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  /var/log/nginx/access.log;
   
       server {
    
    
                  listen 80;
                  Server_name example.com
                  access_log  /var/log/nginx/example.access.log;
                  ...
                  ...
                }
}

3. nginx の設定にすべての変更を加えた後、nginx をリロードし、tail コマンドを実行します。

vim /var/log/nginx/example.access.log

2. nginxのエラーログを表示する

nginx が突然実行を停止したり、正常に動作しなくなったりした場合、すべてのイベントがエラー ログに記録されます。したがって、エラー ログを使用すると、より詳細な情報を見つけることができます。警告もログに記録しますが、発生した問題を特定することはできません。
nginx エラー ログにはさまざまなセキュリティ レベルがあり、エラー ログでは次のセキュリティ レベルを使用できます。
emerg: システムが不安定な場合の緊急メッセージに使用されます
。alert: 重大な問題に対するアラート メッセージを生成します。
crit: 緊急事態における即時処理に使用されます。
error: ページの処理中にエラーが発生した可能性があります。
warn: 警告メッセージの場合
Notice: 無視することもできる通知ログ。
info: 情報については、メッセージ
debug: デバッグ情報のエラーの場所を指します。

エラーログを有効にする

http {
    
    
       ...
       ...
       error_log  /var/log/nginx/error_log;
       #关闭指令
       #error_log off
    
       server {
    
    
                listen 80;
                server_name example1.com;
                    error_log  /var/log/nginx/example1.error_log  warn;
                        ...
       }
       server {
    
    
                listen 8081;
                server_name example2.com;
                    error_log  /var/log/nginx/example2.error_log  debug;
                        ...
   }
}

nginx -t Nginx にエラーがあるかどうかを確認し、エラーが報告されなくなったら再起動します

nginx -t 

nginxをリロードする

service nginx restart

次に、ページを実行し、表示できるエラー ログにエラーを直接報告します。

vim var/log/nginx/example1.error_log  

おすすめ

転載: blog.csdn.net/ihero/article/details/132355115