1. nginxのマルチサーバーの優先順位
HTTPリクエスト処理を満たす場合に、同一であるがnginxの複数の構成、リクエストを処理するサーバのかを決定するために最後に、各nginx.conf SERVER_NAMEに整合、ホスト変数のヘッダnginxのヘッドを取られますserver_nameは、サーバー名、優先度、アクセス違反が発生つながります。
コンフィギュレーション・ファイルを対応する1.1準備nginxの
[root@web01 conf.d]# cat server1.conf
server {
listen 80;
server_name localhost test1.com;
location / {
root /code/test1;
index index.html;
}
}
[root@web01 conf.d]# cat server2.conf
server {
listen 80;
server_name localhost test2.com;
location / {
root /code/test2;
index index.html;
}
}
[root@web01 conf.d]# cat server3.conf
server {
listen 80;
server_name localhost test3.com;
location / {
root /code/test3;
index index.html;
}
}
[root@web01 conf.d]#
1.2サイトの準備ディレクトリ
[root@web01 conf.d]# mkdir /code/test{1..3}
[root@web01 conf.d]# echo test1 > /code/test1/index.html
[root@web01 conf.d]# echo test2 > /code/test2/index.html
[root@web01 conf.d]# echo test3 > /code/test3/index.html
1.3チェック文法のヒントの競合、無視して再起動
[root@web01 conf.d]# nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# nginx -s reload
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
[root@web01 conf.d]#
テストにアクセスするための1.4のブラウザ
#根据域名访问测试正常
#根据ip访问
#1. 用户第一次访问,读取server1.conf配置返回结果
[root@lb01 ~]# curl 10.0.0.5
test1
#2. 此时将server1.conf修改为server4.conf重启nginx
[root@lb01 conf.d]# mv server1.conf server4.conf
[root@lb01 conf.d]# nginx -s reload
#3. 再次访问时,读取server2.conf配置返回结果
[root@lb01 conf.d]# curl 10.0.0.5
test2
以上1.5サーバー名の優先順位の概要
再起動は、HTTPリクエストを処理する際に、nginxのは、それぞれSERVER_NAMEサーバと一致して、タグがリクエストの処理を完了するためにどのとサーバを決定するために、ホストのヘッダ(要求ヘッダ)を読み出し、それがマルチホストで可能です試合中にサーバーserver_nameは、サーバを選択するには、この時間は、実際のマッチング優先順位に従って処理されます。次のように優先順位のマッチング結果は以下の通りでした。
1.まず正確な文字列のサーバー名を選択します。(完全一致)
としてフロントSERVER_NAMEにワイルドカードを選択し.haoda.com www.haoda.com 3. BGXとして、背中にserver_nameのワイルドカードを選択します。 Haoda.com haoda.cn
4.最後に、正規表現のマッチングを使用することを選択サーバー名
5.すべてに一致しない場合、あなたは後のCIは、[DEFAULT_SERVER]サーバブロック聞く参加することを選択します
[サーバーのブロックに一致するポート・コンフィギュレーション・ファイルを見つけるために、耳を傾け、あなたが書いていない場合6.
注:同じような状況は、優先順位が呼び出されますソート複数の設定ファイルをに、server_name、それは同じポート、別のドメイン名を設定することを推奨されている場合、そのようなドメイン名の競合は発生しません。
2. nginxのは、IPへの直接アクセスを禁止します
あなたはサイトに持っているIPまたは未知のドメイン名にアクセスすることにより、ユーザは、あなたが任意の有効なコンテンツを抑制したい場合は、あなたが彼に500への復帰を与えることができ、多くの国内の部屋は、サイトがドメイン名の非ファイリングがトラブルを起こしに来て指し示す防止、空のホストヘッダーを閉じ必要
2.1 nginxの禁止IPアクセス
[root@lb01 conf.d]# cat server4.conf
server {
listen 80 default_server; #默认优先返回;
server_name _; #空主机头或者IP;
return 500; #直接返回500错误;
}
IPマスタードメインにアクセス直接ジャンプする2.2排水方法
[root@lb01 conf.d]# cat server4.conf
server {
listen 80 default_server;
server_name _;
return 302 http://test1.com;
}
3. nginxのは、ファイルをインクルード
サーバーの構成複数のサイト、それらがメインの設定ファイルは非常に大きく、非常に悪い可読性となっnginx.confマスター設定ファイルになりますnginx.conf構成で書かれている場合。そして、後にメンテナンスが面倒になります。今すぐ実行する方法を、素早くサイトをシャットダウンするとしますか?1.それはnginx.confで書かれている場合は、手動で注釈を付ける必要があり、2.それはあまりにも面倒であれば仕方が含まれ、役割を完了するために、設定ファイルを変更するための唯一の拡張は、メインの設定ファイルを簡単にするために含まコメントを含めます、人間が読める容易にします。
使用されるコンフィギュレーションラインは/etc/nginx/online/*.conf#をinlcude
の/ etc / nginxの/オフライン#設定を保存する(オンラインに移動するには、次の使用を)有効になっていません
4. nginxのパスとルートの別名
ルート及びエイリアスnginxのは、2つの異なる方法を行う場所のURIは、それぞれ、ファイルサーバに要求をマッピングする方法後述前記パスマッチングの主な違いは、ディレクトリエイリアスがエイリアスされ、ルートディレクトリは最上位に定義されています。
ルート処理結果である:+ロケーション処理結果のルートパスエイリアスパスは:別名を使用して定義されたパス
4.1を使用するルートは、ユーザーがhttp://image.com/picture/1.jpgにアクセスしたとき、実際には、nginxのは1.JPGを見つけるために、次/コード/画像/ファイルのディレクトリに移動します
[root@lb01 conf.d]# cat image.conf
server {
listen 80;
server_name image.com;
location /picture {
root /code;
}
}
4.2エイリアスを使用している場合、ユーザーはhttp://image.com/picture/1.jpgにアクセスし、実際には、nginxのは1.JPGファイルを見つけるために、次/コード/ディレクトリに移動します
[root@lb01 conf.d]# cat image.conf
server {
listen 80;
server_name image.com;
location /picture {
alias /code;
}
}
4.3一般的な設定オンライン
server {
listen 80;
server_name image.oldboy.com;
location / {
root /code;
}
location ~* ^.*\.(png|jpg|gif)$ {
alias /code/images/;
}
}
5. nginxのtry_file一致パス
nginxののtry_fileパスマッチング、nginxのは、順次ファイルおよびディレクトリが存在するチェックし、最初に見つかったファイルをサービス提供(完全なパラメータ設定指示に基づいて、ルートとエイリアスファイルのパスを設定します)。要素名の後にスラッシュを追加/これがディレクトリであることを示しています。ファイルやディレクトリが存在しない場合は、nginxのは、最後のパラメータは、URIのURIを定義するにジャンプする内部リダイレクトコマンドを実行します。
構成例1 5.1 nginxのtry_file
#1. 配置nginx
[root@lb01 conf.d]# vim try.conf
server {
listen 80;
server_name try.haoda.com;
root /code;
index index.html;
location / {
try_files $uri /404.html;
}
}
#2. 创建实例目录与文件
[root@lb01 conf.d]# echo try11111 > /code/index.html
[root@lb01 conf.d]# echo '404 404 404' > /code/404.html
#3. 尝试访问try.haoda.com
[root@lb01 conf.d]# curl try.haoda.com
404 404 404
#由于访问的是try.haoda.com,而$uri取得是域名后面我们写的内容,它找不到,所以返回后面的内容,即404.html
#4. 尝试访问try.haoda.com/index.html
[root@lb01 conf.d]# curl try.haoda.com/index.html
try11111
#由于访问的是try.haoda.com/index.html,而$uri取到了index.html所以返回/code/index.html的内容
#5. 修改配置为
location / {
try_files $uri $uri/ /404.html;
}
#6. 再次尝试访问try.haoda.com
[root@lb01 conf.d]# curl try.haoda.com
try11111
#我们访问的是try.haoda.com,而$uri我们没有写任何内容,于是他访问的便是“空/”,即匹配到/code/index.html
例えば
location /images/ {
try_files $uri $uri/ /404.html;
}
ユーザーはまず、この場所のtry.haoda.com/images/image1.gif,Nginxでリクエストしたローカルディレクトリ内のファイルを探します。「image1.gif」ファイルが存在しない場合は、nginxのは「image1.gif /」ディレクトリ、「try.haoda.com/images/image1.gif/を」見える存在しない場合は、/ 404」にリダイレクトされます。 HTML "
構成例2 5.2 nginxのtry_file
#1. 配置nginx
[root@lb01 conf.d]# cat try.conf
server {
listen 80;
server_name try.haoda.com;
root /code;
index index.html;
location / {
try_files $uri $uri/ @java; #当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,但一定要加@
}
location @java {
proxy_pass http://172.16.1.8:8080; #配置后端tomcat
}
}
#2. 配置后端tomcat
[root@web02 ~]# cd /usr/share/tomcat/webapps/ROOT
[root@web02 ROOT]# echo 'i am tomcat' > index.html
[root@web02 ROOT]# systemctl start tomcat
#3. 把文件都挪走
[root@lb01 code]# mv index.html index1.html /tmp/
#4. 测试访问
[root@lb01 code]# curl http://try.haoda.com/index.html
i am tomcat
6. nginxのは、アップロードファイルのサイズを調整します
nginxのアップロードファイルを使用するプロセスでは通常、あなたは、413要求エンティティが大きすぎを避けるために、サイズ制限の絶縁を設定する必要があります
nginxのアップロードファイルサイズ制限の設定構文
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location
nginxのロングパス構成例のファイルサイズ制限
#也可以放入http层,全局生效
server {
listen 80;
server_name _;
client_max_body_size 200m;
}
7. nginxの優雅なディスプレイエラーページ
error_pageエラーログ
7.1第一の構成(ネットワークアドレスジャンプ)
#error_page配置的是http这种的网络地址
[root@lb01 conf.d]# cat error.conf
server {
listen 80;
server_name www.haoda.com;
root /code;
#error_page 404 http://www.baidu.com;
location / {
index index.html;
error_page 404 http://www.baidu.com;
}
}
7.2第二の構成は、(ローカルアドレスをジャンプ)
[root@lb01 conf.d]# cat error.conf
server {
listen 80;
server_name error.haoda.com;
root /code;
location / {
index index.html;
}
#error_page 403 404 /404.jpg;
error_page 403 404 /404.html;
location = /404.html {
root /code;
index index.html;
}
}