記事ディレクトリ
序文
- さまざまなWebサーバーソフトウェアの中には、Apache HTTPサーバーに加えて、軽量のHTTPサーバーソフトウェアであるNginxもあります。その安定した効率的な機能は、ますます多くのユーザーに徐々に認識されています。
- それは次のようになりますソースコード、その安定性、豊富な機能セット、簡単な設定ファイルと低システムリソースの消費量との、形で発行されたBSDライセンスに有名な
- その特徴は次のとおりです。メモリが少なく、同時実行性が高い
- 中国本土では、nginx Webサイトのユーザーには、Baidu、JD、Sina、Netease、Tencent、Taobaoなどが含まれます。
1つ:Nginxサービス基盤
1.1:Nginxの概要
-
高性能で軽量なWebサービスソフトウェア
●高い安定性
●システムリソースの消費量が少ない
●同時HTTP接続の高い処理能力
●1台の物理サーバーで30000〜50000の同時リクエストをサポートできます
●占有メモリが少なく、同時実行性が高い
1.2:Nginxのコンパイルとインストール
設置環境
CentOs 7.6 Nginx 1.15
- サポートソフトウェアをインストールする
[root@localhost opt]# iptables -F
[root@localhost opt]# setenforce 0
[root@localhost opt]# mkdir LNMP
[root@localhost opt]# cd LNMP/
#把包移到opt目录中
[root@localhost LNMP]# rz -E
rz waiting to receive.
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
ncurses-5.6.tar.gz
[root@localhost LNMP]# tar zxvf nginx-1.12.2.tar.gz -C /opt
- インストール環境に依存するパッケージ
[root@localhost nginx-1.12.2]# yum -y install gcc gcc-c++ zlib-devel pcre pcre-devel
- 管理用のユーザーとグループを作成する
-M:不创建家目录 -s:用于登录shell
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# id nginx
uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
- コンパイルしてインストール
[root@localhost nginx-1.12.2]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@localhost nginx-1.12.2]# ./con
conf/ configure contrib/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \ #设置安装路径
> --user=nginx \ #运行用户和组设为nginx
> --group=nginx \
> --with-http_stub_status_module #启动模块来支持状态统计
#编译
[root@localhost nginx-1.12.2]# make && make install
- パスの最適化
- Nginxサーバーの操作をより便利にするために、メインプログラムnginxのソフトリンクファイルを作成して、管理者が「nginx」コマンドを実行してNginxのメインプログラムを呼び出すことができるようにすることができます。
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.12.2]# ls /usr/local/bin/
nginx
#测试语法是否正确
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
##查看主配置文件
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
[root@localhost nginx]# cd conf/
#nginx.conf就是主配置文件
[root@localhost conf]# ls
fastcgi.conf nginx.conf
fastcgi.conf.default nginx.conf.default
fastcgi_params scgi_params
fastcgi_params.default scgi_params.default
koi-utf uwsgi_params
koi-win uwsgi_params.default
mime.types win-utf
mime.types.default
#html是主页文件
[root@localhost conf]# cd ..
[root@localhost nginx]# ls
client_body_temp html sbin
conf logs scgi_temp
fastcgi_temp proxy_temp uwsgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# netstat -anpt | grep nginx
##服务没有开启
-
サービスを開始
-
Nginxを直接実行して、Nginxサーバーを起動します
[root@localhost html]# nginx
[root@localhost html]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 90530/nginx: master
- ホスト訪問
- pkillを使用してプロセスを閉じます(すべてのサービスプロセスを閉じます)
[root@localhost html]# pkill nginx
[root@localhost html]# netstat -anpt | grep nginx
- 開始、構成の再読み込み、Nginxの停止
[root@localhost ~]# nginx '#启动服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7180/nginx: master
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://localhost 3显示"Welcome to nginx!"页面,表明Nginx服务已经正常运行'
[root@localhost ~]# killall -s HUP nginx #-S选项指定信号种类,HUP信号表示重载配置'
[root@localhost ~]# killall -s QUIT nginx #QUIT信号表示退出进程
- Nginxがシステムサービスとして追加されました
[root@localhost ~]# vim /lib/systemd/system/nginx.service #添加使用systemctl工具进行管理
[Unit]
Description=nginx #描述
After=network.target #描述服务类别
[Service]
Type=forking #后台运行形势
PIDFile =/usr/local/nginx/logs/nginx.pid PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx 启动服务
ExecReload=/usr/bin/kill -S HUP $MAINPID 根据PID重载配置
ExecStop=/usr/bin/kill -S QUIT $MAINPID 根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
- サービスツール管理もあります
[root@localhost ~]# cd /etc/inid.d 添加使用service工具进行管理
[root@localhost init.d]# ls
[root@localhost init.d]# vim nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on
2:統計ページを構成します
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
####添加下面的图片内容
#在重启服务
[root@localhost init.d]# service nginx stop
[root@localhost init.d]# service nginx start
[root@localhost init.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost init.d]# netstat -ntap | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 90311/nginx: master
- ホストテストに移動
- 更新すると統計が作成されます
3:仮想ホストを構成する
3.1:Nginx仮想ホストアプリケーション
-
Nginxでサポートされる仮想ホストには3つのタイプがあります
●ドメイン名に基づく仮想ホスティング
●IPベースの仮想ホスト
●ポートベースの仮想ホスト
-
「server {}」構成セクションを介して
3.2:ドメイン名に基づく仮想Webホスティング
- ウェブサイトディレクトリとテストファイルを準備する
[root@localhost init.d]# cd /var/
[root@localhost var]# ls
account crash games lib log opt spool yp
adm db gopher local mail preserve target
cache empty kerberos lock nis run tmp
[root@localhost var]# mkdir www
[root@localhost var]# cd www/
#新建两个站点
[root@localhost www]# mkdir shuai mei
[root@localhost www]# ls
mei shuai
[root@localhost www]# cd shuai/
[root@localhost shuai]# vim index.html
#添加以下内容
<h1>hell</h1>
[root@localhost shuai]# cd ../
[root@localhost www]# ls mei/
[root@localhost mei]# vim index.html
#添加网页内容
<h1>this is mei web</h1>
#下载tree
[root@localhost www]# yum -y install tree
[root@localhost www]# tree ./
./
├── mei
│ └── index.html
└── shuai
└── index.html
2 directories, 2 files
#ドメイン名の解決を構成する
[root@localhost www]# vim /etc/named.conf
#Areaconfiguration
エリアデータを構成する
[root@localhost named]# cp -p named.localhost abc.com.zone
[root@localhost named]# vim abc.com.zone
[root@localhost named]# cp -p abc.com.zone ab.com.zone
#重启服务
[root@localhost named]# systemctl start named
- 構成ファイルを変更する
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
server_name www.shuai.com;
location / {
root /var/www/shuai;
index index.html index.php;
}
}
server {
server_name www.shi.com;
location / {
root /var/www/shi;
index index.html index.php;
}
}
- サービスを再開します
[root@localhost named]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost named]# service nginx stop
[root@localhost named]# service nginx start
- クライアントテスト
3.3:ポートベースの仮想Webホスト
-
構成手順
-
構成ファイルを変更する
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
server_name 20.0.0.41:8080; ## 将域名修改为IP地址+端口
listen 20.0.0.41:8080; ## 增加这一行,内容为IP地址+监听端口
location / {
root /var/www/shuai;
index index.html index.php;
}
}
server {
server_name 20.0.0.41:80; ## 同上
listen 20.0.0.41:80; ## 同上
location / {
root /var/www/shi;
index index.html index.php;
}
}
3.4:IPベースの仮想ホスト構成
- ネットワークカードを追加し、IPアドレス(20.0.0.50)を構成します
nginx構成ファイルを編集する
[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf ## 编辑配置文件
server {
server_name 20.0.0.41:80; ## IP地址+80端口
listen 20.0.0.41:80; ## IP地址+80端口
location / {
root /var/www/shuai;
index index.html index.php;
}
}
server {
server_name 20.0.0.50:80; ## 另一个IP地址+80端口
listen 20.0.0.50:80; ## 另一个IP地址+监听端口80
location / {
root /var/www/shi;
index index.html index.php;
}
}
4:Nginxアクセス制御
4.1:承認ベースのアクセス制御
-
設定手順は基本的にApacheと同じです
●ユーザーパスワード認証ファイルを生成する
●メイン設定ファイルを対応するディレクトリに変更し、認証設定項目を追加します
●サービス再開、アクセステスト
-
ユーザーパスワード認証ファイルを生成する
[root@localhost shuai]# yum -y install httpd
[root@localhost shuai]# which htpasswd
/usr/bin/htpasswd
#创建用户
[root@localhost shuai]# htpasswd -c /usr/local/nginx/passwd.db liu
New password: #输入密码
Re-type new password: #再次输入密码
Adding password for user liu
#把账户树组变为nginx
[root@localhost shuai]# chown nginx /usr/local/nginx/passwd.db
#设置权限
[root@localhost shuai]# chmod 400 /usr/local/nginx/passwd.db
- メイン構成ファイルを対応するディレクトリに変更し、認証構成アイテムを追加します
[root@localhost shuai]# vim /usr/local/nginx/conf/nginx.conf
##验证语法
[root@localhost shuai]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#关闭服务在开启
[root@localhost shuai]# service nginx stop
[root@localhost shuai]# service nginx start
クライアントテスト
4.2:クライアントベースのアクセス制御
-
クライアントIPアドレスを介したページへのアクセスを許可するかどうかを決定します
-
構成ルール
IP / IPセグメントの拒否:特定のIPまたはIPセグメントへのクライアントアクセスを拒否します
IP / IPセグメントを許可する:特定のIPまたはIPセグメントのクライアントアクセスを許可します
ルールは上から下に実行され、一致すると停止し、一致しなくなります
-
構成手順
- メイン構成ファイルnginx.confを変更し、対応する構成アイテムを追加します
[root@localhost shuai]# vim /usr/local/nginx/conf/nginx.conf