■プロキシ設定
・proxyモジュール?
組込みモジュールか外部モジュールか?
→nginxのモジュールは、ほとんどが組込モジュールでnginx その2でやったように「nginx -V」で表示できる。
一方、動的モジュールは後から組み込むことができて、/usr/lib64/nginx/modulesに配置されて、nginxデーモン起動時に動的リンクする。
apacheの場合はmod_proxyというDSOだけど、nginxのproxy機能はコアコンポーネントに含まれているっぽい(調査中)
・プロキシキャッシング
--調査中--
・プロキシ関連のディレクティブ
■proxy_set_headerディレクティブ
【場所】serverコンテキスト
【説明】
【構文】
proxy_set_header ヘッダ名 値;
【例】
proxy_set_header Host $host; ←「$host」は環境変数? nginxにも環境変数あるの?
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
■ ---調査中---
■
・静的Webサーバへのリバースプロキシ
・前提条件
server{
server_name test.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location /test1/ { # URL転送先
proxy_pass http://192.168.2.26:80/test/;
}
location / { # ローカルコンテンツ
charset utf-8;
root /usr/share/nginx/html/test;
index index.html index.htm;
}
}
<html>
<head>
<title>test2</title>
<meta charset="utf-8"/>
</head>
<body>
<H1><font color="blue">これはCentOS7のtestの中のindex.htmlだお!</font></H1>
</body>
</html>
<html>
<head>
<title>test1</title>
</head>
<body>
<H1><font color="red">これはCentOS6のtestの中のindex.htmlだお!</font></H1>
</body>
</html>
これはCentOS6のtestの中のindex.htmlだお!
これはCentOS7のtestの中のindex.htmlだお!
これはCentOS6のtestの中のindex.htmlだお!
→URL転送先のコンテンツが表示された!
/etc/nginx/conf.d/test.confの記載順序を変更してみる
# cat /etc/nginx/conf.d/test.conf
server{
server_name test.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / { # ローカルコンテンツをURL転送先コンテンツの上に持ってくる
charset utf-8;
root /usr/share/nginx/html/test;
index index.html index.htm;
}
location /test1/ {
proxy_pass http://192.168.2.26:80/test/;
}
# location / {
# charset utf-8;
# root /usr/share/nginx/html/test;
# index index.html index.htm;
# }
}
これはCentOS6のtestの中のindex.htmlだお!
→URL転送先のコンテンツが表示された!
【検証3】
どうやら、nginxのlocationディレクティブは書き順でなく、URLのロンゲストマッチなのか?
→URL転送先のURLをローカルコンテンツのURLよりロングに書き換える
# cat /etc/nginx/conf.d/test.conf
server{
server_name test.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / { # /test1/ → / に変更
proxy_pass http://192.168.2.26:80/test/;
}
location /test1 { # / → /test1/ に変更
charset utf-8;
root /usr/share/nginx/html/test;
index index.html index.htm;
}
}
URL転送先のコンテンツの階層を変える
せ6> cd /var/www/html/test
せ6> mkdir test1
せ6> mv index.html test1
せ6> ll /var/www/html/test/test1
合計 4
-rw-r--r-- 1 root root 152 2月 11 16:48 2018 index.html
→
http://test.com/test1/
これはCentOS7のtestの中のindex.htmlだお!
→ローカルコンテンツが表示された!
【結論】
nginx.confのlocationディレクティブはロンゲストマッチが適用される
まあ、そりゃそうだよね。
ちなみに、URL転送先のコンテンツのパスを下記のように変更
[変更前]
/var/www/html/test/test1/index.html
[変更後
/var/www/html/test/test2/index.html
→
http://test.com/test1/
これはCentOS7のtestの中のindex.htmlだお!
→
http://test.com/test2/
これはCentOS6のtestの中のindex.htmlだお!
※ちなみに、curl -Iコマンドの出力はこんな感じ
せ6> curl -I http://test.com/test2
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sun, 11 Feb 2018 09:48:50 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://test.com/test/test2/
せ6> curl -I http://test.com/test1
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sun, 11 Feb 2018 09:48:54 GMT
Content-Type: text/html
Content-Length: 178
Location: http://test.com/test1/
Connection: keep-alive
・fastcgiサーバへのリバースプロキシ
- nginxではfastcgiやPHPサーバを実行せずフロントエンドとして構成するのが一般的
nginxではapacheのようなcgiスクリプトを実行するモジュールはない。
nginxがcgiスクリプトへのhttpリクエストを受け取った場合は必ずバックエンドcgiサーバにリバースプロキシする。(参照:LPIC Lv.2の教科書)
fastcgiやPHPなどのプログラムはバックエンドのapacheで実行させ、nginxはこのapacheのフロントエンドにする構成が一般的
- nginxでfastcgiやPHPサーバへリバースプロキシする際に設定するディレクティブ
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
・fastcgi_passディレクティブ
・fastcgi_paramディレクティブ
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
---調査中---
・サーブレットコンテナへのリバースプロキシ
下記はapacheからtomcatとの連携、mod_jk、mod_proxy_ajp、mod_proxy_httpの話
https://qiita.com/i_learnin/items/c7e09a3e930016226d93
下記はnginxの話
いろいろすったもんだな話があるみたいwww
[Grails]NginxとTomcatを連携&注意点 - Qiita
Nginx + Tomcatのリバースプロキシでのリダイレクト対応 - Qiita
下記に出てくる
upstream tomcat {
:
}
ってなんだ?
これ見てると、単にtomcatのListening port向けに、proxy_path
Nginx as a Reverse-Proxy to Apache Tomcat
---調査中---
・バックエンドsolrサーバにURL転送
---調査中---