CentOS7の運用と保守-Nginx構成チュートリアル|実行パフォーマンスの向上|アクセス制御|仮想ホスト

1つ、Nginxの概要

高性能で軽量なWebサービスソフトウェア

2.Nginxサービスステータス

①サービス開始

nginx -t構成ファイルの構文構造が番号の正しい
cat /usr/local/nginx/logs/nginx.pidビューnginxであること確認してくださいPID

プロセス番号を表示するその他の方法
lsof-i:80
netstat -natp | grep nginx
ss -tnlp | grep nginx
ps -ef | grep nginx
pgrep nginx

②サービス停止

kill続いpid番号killallプロセス名が続きます

kill -3 <PID号>
kill -s QUIT <PID号>
killall -3 nginx
killall -s QUIT nginx

③過負荷サービス

kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx

④ログ分割

原則:特定の条件が満たされた場合、古いログを保存および削除します

kill -USR1 <PID号>

⑤スムーズなアップグレード

kill -USR2 <PID号>

三、プログラムシステムサービスを追加

方法1:systemctl

echo '
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/nginx.service

方法2:init.d

vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx 服务
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $COM
;;
stop)
  kill -s QUIT ${cat PID}
;;
restart)
  $0 stop
  $0 start
;;
reload)
  kill -s HUP ${cat PID}
;;
*)
  exit 1
esac
exit 0

システムサービスを追加し、実行権限を付与します

chmod +x /etc/init.d/nginx
chkconfig --add nginx

4、Nginxメイン構成ファイル

user_nobody実行中のユーザーは、コンパイル時に指定されていない場合、デフォルトはnobdy
worker_processes 1;ワークプロセスの数は次のように構成できます内核数 * 2[通常は1で十分です]
error_log logs/error.log;エラーログ
pid logs/nginx.pid PIDファイルの場所の場所

①パフォーマンスを向上させる方法

I / Oイベント構成

events {
    
    
  use epoll;
  #2.6以上版本的系统核心建议使用epoll模型以提高性能
  worker _connections 4095
  #每个进程处理 4096 个连接
}

②プロセスあたりの接続数を増やすにはどうすればよいですか?

Linuxプラットフォームが同時実行性の高いTCP接続を処理している場合、同時接続の最大数は
、ユーザーが1つのプロセスで同時に開くことができるファイルの数に関するシステムの制限の対象となります。TCP接続ごとに1つ作成します。socket
socketハンドルはファイルハンドルでもあります。
ulimit -n 65535各プロセスがローカルで開くこと
ulimit -aができるファイルの最大数を一時的に変更します。現在のシステムユーザーが開くことができるファイルの最大数を表示できます。[open files项]

5、HTTP構成

http {
    
    
    ##文件扩展名与文件类型映射表
    include     mime.types;
    ##默认文件类型
    default_type  application/octet-stream;
    ##日志格式设定
    #log_format  main   '$remote_ addr - $remote. user [stime_ local] "$request" '
    #                   '$status $body_ bytes_ sent "Shttp_ referer" '
    #                   "$http_user_agent" "$http_x_forwarded_for"';
    ##访问日志位置
    #access_Log logs/access.log main;
    #支持文件发送(下载) 
    sendfile    on;
    ##此选项允许或禁止使用socket的TCP_CORK的选项( 发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
    #tcp_nopush        on;
    ##连接保持超时时间,单位是秒
    #keepalive_timeout  0;
    keepalive.timeout  65;
    ##gzip模块设置,设置是否开启gzip压缩输出
    #gzip   on; 

    #Web服务的监听配置
    ##Web服务的监听配置
    server (
        ##监听地址及端口
        listen 80;
        ##站点域名,可以有多个,用空格隔开
        server.name www.benet.com;
        ##网页的默认字符集
        charset utf-8;
        ##根目录配置
        location / {
    
    
            ##网站根目录的位置/usr/local/nginx/html
            root html;
            ##默认首页文件名
            index index.html index.php;
        }
        ##内部错误的反馈页而
        error_page 500 502 503 504 /50x.html;
        #错误页而配置
        location = /50x.html {
    
    
            root html;
        }
    }
}

6、ログ形式の設定

$remote_addr$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user: 用来记录客户端用卢名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态,成功是200;
$body_bytes_sent: 记录发送给客户端文件主体内容大小;
$http_referer: 用来记录从那个页面链接访问过来的;
$http_user_agent: 记录客户浏览器的相关信息;

通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的IP地址。反问代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

##location常见配置指令,root、alias、proxy_pass

root(根路径配置): 请求www.benet.com/1.jpg,会返回文件/usr/local/nginx/html/test/1.jpg
alias(别名配置): 请求ww . benet.com/test/1.jpg,会返回文件/usr/local/nginx/html/1.jpg
proxy_pass(反向代理配置>;
proxy_pass http://127.0.0.1:8080/;    会转发请求到http://127.0.0.1:8080/1.jpg
proxy_pass http://127.0.0.1:8080;

7、アクセスステータス統計

HTTP_STUB_STATUSモジュールがあるかどうかを確認します

nginx -t

nginxメインの構成ファイルを入力して追加しますstub_status

cd /usr/local/nginx/conf
cp nginx.conf{
    
    ,.bak}
vim /usr/local/nginx/conf/nginx.conf
.....
http {
    
    
.....
  server {
    
    
    listen 80;
    server_name www.benet.com;
    charset utf-8;
    location / {
    
    
      root html;
      index index.html index.html;
    }
    ##添加stub_status 配置##
    location /status {
    
          #访问位置为/status
      stub_status on;       #打开状态统计功能
      access_log off;       #关闭此位置的日志记录
    }
  }
}

8、Nginxアクセス制御

①承認に基づくアクセス制御

ユーザーパスワードの生成認証ファイル
オンラインで生成できます

yum install -y httpd-tools

htpasswd -c /usr/local/nginx/passwd.db fox

通常のユーザーの権限を削除する場合は、ファイルの所有者を設定する必要があります

chown nginx /usr/local/nginx/passwd.db
chmod 700 /usr/local/nginx/passwd.db

メイン構成ファイルの対応するディレクトリを変更し、認証構成アイテムを追加します

vim /usr/local/nginx/conf/nginx.conf
......
  server {
    
    
    location / {
    
    
      ......
      ##添加认证配置##
      auth_basic "secret" ;
      auth_basic_user_file /usr/local/nginx/passwd.db;
    }
  }

②クライアントベースのアクセス制御

アクセス制御ルールは次のとおりです
。IP/ IPセグメントの拒否:特定のIPまたはIPセグメントのクライアントアクセスを拒否します
。IP/ IPセグメントの許可:特定のIPまたはIPセグメントのクライアントアクセス
ルールを上から下に実行できるようにします一致する場合は停止します。一致しなくなります

vim /usr/local/nginx/conf/nginx.conf
    server {
    
    
        location / {
    
    
        #添加控制规则
        deny 192.168.0.10;
        #拒绝访问的客户端IP
        allow all;
        #允许其它IP客户端访问
        }
    }
    
systemctl restart nginx

9、Nginx仮想ホスト

①ドメイン名に基づく仮想ホスティング

echo "192.168.0.50 www.benet.com www.accp.com" >> /etc/hosts

►仮想ホスティング用のWebドキュメントを準備する

mkdir -p /var/www/html/accp
mkdir -p /var/www/html/benet
echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html
echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html

►Nginx構成ファイルを変更する

vim /usr/local/nginx/conf/nginx.conf
http {
    
    
    server {
    
    
        listen 80;
        server_name www.accp.com;
        charset utf-8;
        access_log logs/www.accp.com.access.log;
        location / {
    
    
            root /var/www/html/accp;
            index index.html index.html;
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
    
    
            root html;
        }
    }

    server {
    
    
        listen 80;
        server_name www.benet.com;
        charset utf-8;
        access_log logs/www.benet.access.com.log;
        location / {
    
    
        root /var/www/html/benet;
        index index.html index.html;
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
    
    
            root html;
        }
    }
}
nginx -t
systemctl restart nginx


②IPベースの仮想ホスト

ifconfig ens33:0 192.168.0.60/24
vim /usr/local/nginx/conf/nginx.conf
http {
    
    
    server {
    
    
        listen 192.168.0.50:80;
        server_name www.benet.com;
        charset utf-8;
        access_log logs/www.benet.access.log;
        location / {
    
    
            root /var/www/html/benet;
            index index.html index.php;
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
    
    
            root html;
        }
    }
    
    server {
    
    
        listen 192.168.0.60:80;
        server_name www.accp.com;
        charset utf-8;
        access_log logs/www.accp.access.log;
        location / {
    
    
            root /var/www/html/accp;
            index index.html index.html;
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
    
    
            root html;
        }
    }
}
nginx -t
systemctl restart nginx


③ポートベースの仮想ホスト

vim /usr/local/nginx/conf/nginx.conf
http {
    
    
    server {
    
    
        listen 192.168.0.50:233;
        server_name www.accp.com;
        charset utf-8;
        access_log logs/www.accp.access.log;
        location / {
    
    
            root /var/www/html/accp;
            index index.html index.html;
        }
        error_page 500 502. 503 504 /50x.html;
        location = 50x.html{
    
    
            root html;
        }
    }

    server {
    
    
        listen 192.168.0.50:8080;
        server_name www.benet.com;
        charset utf-8;
        access_log logs/www.benet.access.log;
        location / {
    
    
            root /var/www/html/benet;
            index index.html index.php; 
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
    
    
            root html;
        }
    }
}
nginx -t
systemctl restart nginx.service

おすすめ

転載: blog.csdn.net/qq_42427971/article/details/115330656