記事ディレクトリ
ブラウザ経由で HTTP リクエストを送信すると、リクエストはクライアントからサーバーに送信され、必要なコンテンツが取得され、そのコンテンツがエコーされてページに表示されます。このとき、リクエストするコンテンツは、静的リソースと動的リソースの 2 種類に分けられます。静的リソースとは、一般的な html ページ、css ファイル、js ファイル、写真、ビデオなどのリソースなど、サーバー側に実際に存在し、直接表示できるファイルを指します。動的リソースは、サーバー側に実際に存在するファイルを指しますが、動的リソースはサーバー側に実際に存在するファイルを指します。特定のビジネス ロジック処理を経て、レポート データの表示、現在ログインしているユーザーに基づく関連する特定のデータやその他のリソースの表示など、さまざまな条件に従ってページのさまざまな部分を表示する必要があります。
Nginx が静的リソースのコンテンツを処理する場合、次の問題を考慮する必要があります。
(1) 静的リソース構成命令
(2) 静的リソース構成の最適化
(3) 静的リソース圧縮構成命令
(4) 静的リソースのキャッシュ処理
(5) 静的リソースのアクセス制御 (クロスドメイン問題およびアンチリーチング問題を含む)
まず、静的リソースの構成手順を見てみましょう。
01.server_nameコマンド
リクエストを受信すると、nginx はまずリクエストのホスト名とポート番号が構成ファイル内のサーバー ブロックのリスニング アドレス (listen) と一致するかどうかを確認します。一致するリスニング アドレスが見つかった場合、nginx はサーバー ブロック内の server_name ディレクティブを引き続きチェックします。server_name ディレクティブは、server ブロックと一致する仮想ホストまたは IP を指定するために使用されます。nginx は次の順序で一致します。
- 特定のホスト名がserver_nameディレクティブで指定されている場合、要求されたホスト名はそのホスト名と正確に一致する必要があります。
- server_name ディレクティブでワイルドカード文字が使用されている場合、要求されたホスト名はワイルドカード文字と一致する必要があります。
- server_name ディレクティブで正規表現が使用されている場合、要求されたホスト名は正規表現と一致する必要があります。
リクエストされたホスト名に一致するサーバー ブロックが見つかった場合、nginx はサーバー ブロック内に設定された対応する場所にリクエストを転送します。要求されたホスト名に一致するサーバー ブロックが見つからない場合、nginx はデフォルトのサーバー ブロックを使用してリクエストを処理します。デフォルトのサーバー ブロックが設定されていない場合、nginx は最初のサーバー ブロックを使用してリクエストを処理します。
server_name には、完全一致、ワイルドカード一致、および正規表現一致という 3 つの構成方法があります。
1.完全一致
完全一致の構文:
server_name domain1.com domain2.com;
server_name 192.168.1.100;
このうち、domain1.comとdomain2.comは仮想ホストのドメイン名であり、スペースで区切って複数指定できます。リクエストされたホスト名がserver_nameで指定されたドメイン名と一致する場合、Nginxはリクエストをこの仮想ホストに渡して処理します。ドメイン名に加えて、server_name には IP アドレスも指定できます。このようにして、要求されたホスト名が 192.168.1.100 である場合、Nginx はこの仮想ホストに要求を渡して処理します。
Hosts は拡張子のないシステム ファイルです。その機能は、一般的に使用される URL ドメイン名とそれに対応する IP アドレスの間に関連する「データベース」を確立することです。ユーザーがブラウザにログインする必要がある URL を入力すると、システムはまず、hosts ファイル内の対応する IP アドレスの検索から自動的に開始されます。見つかると、システムはすぐに対応する Web ページを開きます。見つからない場合は、システムは IP アドレス解決のために URL を DNS ドメイン名解決サーバーに送信します。 。ドメイン名には一定の料金がかかるため、hosts ファイルを変更して、使用する仮想ドメイン名を作成できます。
# 配置hosts文件
[root@192 conf]# cat /etc/hosts
127.0.0.1 www.domain1.com
127.0.0.1 www.domain2.com
# 配置nginx.conf文件
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
server_name 127.0.0.1;
default_type text/plain;
return 500 'This is a 500 request';
}
server {
listen 8080;
server_name www.domain1.com www.domain2.com;
default_type text/plain;
return 444 'This is a 444 request';
}
}
# 测试访问
[root@192 conf]# curl http://www.domain1.com:8080/
This is a 444 request
[root@192 conf]# curl http://www.domain2.com:8080/
This is a 444 request
[root@192 conf]# curl http://127.0.0.1:8080/
This is a 500 request
2. ワイルドカードのマッチング
Server_name はワイルドカードをサポートしています*
が、ワイルドカードはドメイン名の途中には使用できず、最初または最後の段落にのみ使用できることに注意してください。
# 配置nginx.conf文件
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
server_name 127.0.0.1;
default_type text/plain;
return 500 'This is a 500 request';
}
server {
listen 8080;
server_name *.com;
default_type text/plain;
return 444 'This is a 444 request';
}
}
# 测试访问
[root@192 conf]# curl http://www.domain1.com:8080/
This is a 444 request
3. 正規表現によるマッチング
正規表現は、server_name で使用でき、~
正規表現文字列の開始タグとして使用できます。一般的な正規表現:
コード | 説明する |
---|---|
^ | 検索文字列の先頭と一致する |
$ | 検索文字列の末尾と一致する |
。 | 改行を除く任意の 1 文字と一致します \n |
\ | 次の文字を特殊文字としてマークするためのエスケープ文字 |
[xyz] | 文字セット。指定された任意の文字に一致します |
[az] | 文字範囲。指定された範囲内の任意の文字と一致します。 |
\w | 次の文字 AZ、0-9、およびアンダースコアのいずれかと一致します。[A-Za-z0-9_] と同等です。 |
\d | 数字のマッチング、[0-9] に相当 |
{n} | n 回正確に一致 |
{n,} | 少なくとも n 回一致 |
{n,m} | 少なくとも n 回、最大で m 回一致します |
* | 0 回以上、{0,} に相当 |
+ | 1 回以上、{1,} に相当 |
? | 0 回または 1 回、{0,1} に相当 |
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
# 以^开头,以$结尾
server_name ~^www\.(\w+)\.com$;
default_type text/plain;
# $1 匹配的是 (\w+) 中的值
return 444 '=====》$1 =====》';
}
}
# 测试
[root@192 conf]# curl http://www.domain2.com:8080/
=====》domain2 =====》
[root@192 conf]# curl http://www.domain1.com:8080/
=====》domain1 =====》
4.default_server 属性
Nginx では、default_server 属性は仮想ホストを識別するために使用され、その機能はこの仮想ホストをデフォルト ホストとして設定することです。いわゆるデフォルト ホストとは、Nginx がリクエストを受信したときに、対応するサーバー ブロックが一致しない場合、default_server に対応する仮想ホストがリクエストの処理にデフォルトで使用されることを意味します。default_server が設定されていない場合、Nginx は最初のサーバー ブロックを使用してリクエストを処理します。
① 対応するサーバーブロックが一致しない場合、デフォルトでは、default_server に対応する仮想ホストがリクエストの処理に使用されます。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
server_name www.hh.com;
default_type text/plain;
return 444 'This is a 444 request';
}
server {
listen 8080 default_server;
server_name www.hh.com ;
default_type text/plain;
return 500 'This is a 500 request';
}
}
# 如果没有匹配到对应的 server,则会默认使用 default_server 所对应的虚拟主机来处理该请求
[root@192 conf]# curl http://127.0.0.1:8080/
This is a 500 request
[root@192 conf]# curl http://192.168.38.34:8080/
curl: (7) Failed connect to 192.168.38.34:8080; 没有到主机的路由
②default_server が設定されていない場合、Nginx は最初のサーバーを使用してリクエストを処理します。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
server_name www.hh.com;
default_type text/plain;
return 444 'This is a 444 request';
}
server {
listen 8080;
server_name www.hh.com;
default_type text/plain;
return 500 'This is a 500 request';
}
}
# 如果没有设置 default_server,则 Nginx 会使用第一个 server 来处理该请求
[root@192 conf]# curl http://127.0.0.1:8080/
This is a 444 request
5. マッチングオーダーの場合
server_name ディレクティブはワイルドカードと正規表現をサポートしているため、複数のサーバー ブロックを含む構成ファイルでは、名前が複数の仮想ホストの server_name と正常に一致する可能性があります。この場合、現在のリクエストは誰が処理しますか?
①server_name が完全一致、正規表現一致、先頭のワイルドカード一致、末尾のワイルドカード一致、およびデフォルト設定であるサーバー ブロックがあります。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name www.domain1.com;
default_type text/plain;
return 200 '精确匹配成功';
}
server{
listen 8080;
server_name ~^www\.\w+\.com$;
default_type text/plain;
return 200 '正则表达式匹配成功';
}
server{
listen 8080;
server_name www.domain1.*;
default_type text/plain;
return 200 '通配符匹配*成功';
}
server{
listen 8080;
server_name *.domain1.com;
default_type text/plain;
return 200 '*通配符匹配成功';
}
server{
listen 8080 default_server;
server_name _;
default_type text/plain;
return 444 'default_server未找到server匹配成功';
}
}
[root@192 conf]# curl http://www.domain1.com:8080/
精确匹配成功
② 正規表現に一致するserver_name、先頭にワイルドカードが一致、末尾にワイルドカードが一致し、デフォルト設定が設定されたサーバー ブロックがあります。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name ~^www\.\w+\.com$;
default_type text/plain;
return 200 '正则表达式匹配成功';
}
server{
listen 8080;
server_name www.domain1.*;
default_type text/plain;
return 200 '通配符匹配*成功';
}
server{
listen 8080;
server_name *.domain1.com;
default_type text/plain;
return 200 '*通配符匹配成功';
}
server{
listen 8080 default_server;
server_name _;
default_type text/plain;
return 444 'default_server未找到server匹配成功';
}
}
[root@192 conf]# curl http://www.domain1.com:8080/
*通配符匹配成功
③ 正規表現に一致するserver_name、末尾のワイルドカード一致、およびデフォルト設定を含むサーバーブロックがあります。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name ~^www\.\w+\.com$;
default_type text/plain;
return 200 '正则表达式匹配成功';
}
server{
listen 8080;
server_name www.domain1.*;
default_type text/plain;
return 200 '通配符匹配*成功';
}
server{
listen 8080 default_server;
server_name _;
default_type text/plain;
return 444 'default_server未找到server匹配成功';
}
}
[root@192 conf]# curl http://www.domain1.com:8080/
通配符匹配*成功
④server_name が正規表現一致でデフォルト設定であるサーバー ブロックがあります。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name ~^www\.\w+\.com$;
default_type text/plain;
return 200 '正则表达式匹配成功';
}
server{
listen 8080 default_server;
server_name _;
default_type text/plain;
return 444 'default_server未找到server匹配成功';
}
}
[root@192 conf]# curl http://www.domain1.com:8080/
正则表达式匹配成功
⑤ デフォルト設定のサーバーブロックと一致させます。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080 default_server;
server_name _;
default_type text/plain;
return 444 'default_server未找到server匹配成功';
}
}
[root@192 conf]# curl http://www.domain1.com:8080/
default_server未找到server匹配成功
02.リッスンコマンド
公式ドキュメントのアドレス: https://nginx.org/en/docs/http/ngx_http_core_module.html#listen
Nginx では、listen ディレクティブを使用して、Nginx がリッスンする IP アドレスとポート番号を指定します。その構文は次のとおりです。
listen address:port [options];
このうち、address は監視対象の IP アドレスを表し、特定の IP アドレスまたはワイルドカード文字 (使用可能なすべての IP アドレスの監視を示す * など) を使用できます。port は監視対象のポート番号を示します。options はオプションであり、SSL/TLS 構成、HTTP/2 構成などの追加オプションを指定するために使用されます。
一般的な listen ディレクティブの例をいくつか示します。
# 监听指定的IP和端口
listen 127.0.0.1:8000;
# 监听指定IP的所有端口
listen 127.0.0.1;
# 监听所有可用的 IP 地址,端口号为 8000
listen 8000;
# 监听所有可用的 IP 地址,端口号为 80
listen *:80;
# 监听指定的 IP 地址,端口号为 8080,启用 SSL/TLS
listen 192.168.1.100:8080 ssl;
# 监听指定的 IP 地址,端口号为 443,启用 SSL/TLS 和 HTTP/2
listen 192.168.1.100:443 ssl http2;
① nginx 設定ファイルを変更します。仮想ホストの 8080 ポートをリッスンします。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
server_name 127.0.0.1;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
②テストアクセス:http://192.168.38.33:8080/
03. 場所ディレクティブ
location ディレクティブは、要求された URI を照合し、一致した要求を処理する方法を指定するために使用されます。location ディレクティブの構文は次のとおりです。
location [ = | ~ | ~* | ^~ ] uri {
...
}
このうち、uri には、要求された URI と一致するために使用される文字列または正規表現を指定できます。= は完全一致を表し、大文字と小文字を区別する正規一致を表します。 * は大文字と小文字を区別しない正規一致を表し、^~ は通常の文字列一致を表します。リクエストが Nginx に到着すると、最初に一致するディレクティブが見つかるまで、特定の順序で location ディレクティブを 1 つずつ照合します。位置ディレクティブの一致順序は次のとおりです。
① 完全一致 ( =
): 要求された URL パスが指定された文字列と完全に一致する場合、この構成ブロックは要求を処理するために使用されます。例えば:
location = /path {
...
}
② プレフィックス マッチング ( ^~
): リクエストされた URL パスが指定された文字列で始まる場合、この設定ブロックを使用してリクエストを処理し、他のロケーション ブロックのマッチングを停止します。例えば:
location ^~ /path {
...
}
③ 正規表現一致 (~
または~*
): 要求された URL パスが指定された正規表現と一致する場合、この構成ブロックは要求を処理するために使用されます。~
大文字と小文字を区別することを示し、~*
大文字と小文字を区別しないことを示します。例えば:
location ~ /path {
...
}
④ 通常の文字列マッチング: リクエストされた URL パスに指定された文字列が含まれる場合、この設定ブロックを使用してリクエストが処理されます。例えば:
location /path {
...
}
1. 完全一致 (=)
要求された URL パスが指定された文字列と正確に一致する場合、要求はこの構成ブロックを使用して処理されます。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location = /path {
default_type text/plain;
return 200 'access success';
}
}
}
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/path
access success
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/path?a=zhangsan
access success
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/pathabc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/path/abc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
2. 前方一致 (^~)
要求された URL パスが指定された文字列で始まる場合、要求はその構成ブロックを使用して処理され、他の場所ブロックの一致は停止されます。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location ^~ /path {
default_type text/plain;
return 200 'access success';
}
}
}
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/path
access success
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/pathabc
access success
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/path/abc
access success
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/pat
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
3. 正規表現マッチング (~ または ~*)
要求された URL パスが指定された正規表現と一致する場合、この構成ブロックを使用して要求が処理されます。~
大文字と小文字を区別することを示し、~*
大文字と小文字を区別しないことを示します。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
# 正则表达式匹配,~^ 表示后面的路径是以 "/path" 开头,\w 表示匹配一个字母或数字,$ 表示路径的结尾。
location ~^/path\w$ {
default_type text/plain;
return 200 'access success';
}
}
}
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/path2
access success
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/patha
access success
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/Patha
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/pathab
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/path/abc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/path
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
# 正则表达式匹配,~*^ 表示后面的路径是以 "/path" 开头,\w 表示匹配一个字母或数字,$ 表示路径的结尾。
location ~*^/path\w$ {
default_type text/plain;
return 200 'access success';
}
}
}
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/patha
access success
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/path1
access success
# 可以匹配
[root@192 conf]# curl http://192.168.38.33:8080/Patha
access success
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/Path/abc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
# 无法匹配
[root@192 conf]# curl http://192.168.38.33:8080/path
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
4. 通常の文字列マッチング
要求された URL パスに指定された文字列が含まれている場合、この構成ブロックは要求を処理するために使用されます。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location /path {
default_type text/plain;
return 200 'access success';
}
}
}
[root@192 conf]# curl http://192.168.38.33:8080/path
access success
[root@192 conf]# curl http://192.168.38.33:8080/path/
access success
[root@192 conf]# curl http://192.168.38.33:8080/path/abc
access success
[root@192 conf]# curl http://192.168.38.33:8080/path/abc/
access success
5. マッチングオーダーの場合
① 完全一致、前方一致、正規表現一致、通常の文字列一致が存在します。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location = /path {
default_type text/plain;
return 200 '精确匹配';
}
}
server{
listen 8080;
server_name localhost;
location ^~ /path {
default_type text/plain;
return 200 '前缀匹配';
}
}
server{
listen 8080;
server_name localhost;
location ~ /path {
default_type text/plain;
return 200 '正则表达式匹配';
}
}
server{
listen 8080;
server_name localhost;
location /path {
default_type text/plain;
return 200 '普通字符串匹配';
}
}
}
[root@192 conf]# curl http://192.168.38.33:8080/path
精确匹配
② 前方一致一致、正規表現一致、通常の文字列一致があります。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location ^~ /path {
default_type text/plain;
return 200 '前缀匹配';
}
}
server{
listen 8080;
server_name localhost;
location ~ /path {
default_type text/plain;
return 200 '正则表达式匹配';
}
}
server{
listen 8080;
server_name localhost;
location /path {
default_type text/plain;
return 200 '普通字符串匹配';
}
}
}
[root@192 conf]# curl http://192.168.38.33:8080/path
前缀匹配
③ 正規表現マッチングと通常の文字列マッチングがある
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location ~ /path {
default_type text/plain;
return 200 '正则表达式匹配';
}
}
server{
listen 8080;
server_name localhost;
location /path {
default_type text/plain;
return 200 '普通字符串匹配';
}
}
}
[root@192 conf]# curl http://192.168.38.33:8080/path
正则表达式匹配
04. rootコマンド
rootディレクティブは、Nginxサーバーのルートディレクトリを指定するディレクティブです。Nginx 設定ファイルでは、root ディレクティブを使用してサーバーのルート ディレクトリを指定できます。その構文は次のとおりです。
root path;
ここで、 path はサーバーのルート ディレクトリへのパスです。絶対パスまたは相対パスを使用できます。相対パスを使用する場合、Nginx 構成ファイルのパスに対する相対パスになります。
/usr/local/nginx/html
そのディレクトリの下に画像ディレクトリを作成し、a.jpg、b.jpg
そのディレクトリに 2 つの画像を置きます。
① 相対パスを使用します。
[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg b.jpg
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location /image {
root html;
}
}
}
クライアントが http://192.168.38.33:8080/image/b.jpg をリクエストすると、/usr/local/nginx/html/image/b.jpg にアクセスします。
② 絶対パスを使用する: ルートの処理結果は、ルートパス + ロケーションパス、/usr/local/nginx/html/image/b.jpg となります。
[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg b.jpg
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location /image {
root /usr/local/nginx/html;
}
}
}
クライアントが http://192.168.38.33:8080/image/b.jpg をリクエストすると、/usr/local/nginx/html/image/b.jpg にアクセスします。これは、root で指定されたパス + で指定されたパスです。位置。
05. エイリアスコマンド
エイリアス ディレクティブは、URL パスをサーバーのファイル システム内の別の場所にマップします。その構文は次のとおりです。
location /path {
alias /new/path;
}
このうち、/path はクライアントが要求した URL パス、/new/path はサーバー ファイル システム内の実際のパスです。クライアントが /path をリクエストすると、Nginx はそれを /new/path にマップし、対応するファイルまたはディレクトリを返します。
①/usr/local/nginx/html
ディレクトリの下に画像ディレクトリを作成し、a.jpg、b.jpg
そのディレクトリに 2 枚の写真を置きます。
[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg b.jpg
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location /image {
alias /usr/local/nginx/html;
}
}
}
クライアントが http://192.168.38.33:8080/image/b.jpg をリクエストすると、エラー 404 Not Found が報告されます。
nginx エラー ログを確認します。
[root@192 conf]# tail -f /usr/local/nginx/logs/error.log
2023/09/03 16:07:02 [error] 91629#0: *87 open() "/usr/local/nginx/html/b.jpg" failed (2: No such file or directory), client: 192.168.38.1, server: localhost, request: "GET /image/b.jpg HTTP/1.1", host: "192.168.38.33:8080"
aliasで指定したパスである/usr/local/nginx/html/b.jpgにアクセスしていることが分かりますが、locationで指定したパスの代わりにaliasで指定したパスが使用されます。
② 設定ファイル内のエイリアスで指定したパスを再変更します。
[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg b.jpg
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location /image {
alias /usr/local/nginx/html/image;
}
}
}
クライアントが http://192.168.38.33:8080/image/b.jpg をリクエストすると、アクセスは /usr/local/nginx/html/image/b.jpg になります。
06.インデックスコマンド
Index ディレクティブは、Web サイトのウェルカム ページを設定するために使用されます。その構文は次のとおりです。
index file1 [file2 ...];
このうち、file1、file2などはインデックスファイル名であり、スペースで区切って複数指定できます。nginx は、これらのファイルが見つかるまで、指定された順序で検索します。どちらも見つからない場合は、403 Forbidden エラーが返されます。
[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg b.jpg
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
location /image {
root /usr/local/nginx/html;
index b.jpg a.jpg;
}
}
}
クライアントが http://192.168.38.33:8080/image/ をリクエストすると、インデックス ディレクティブを使用してデフォルト ファイル b.jpg が指定されているため、Nginx は /usr/local/nginx/html/image/b.jpg を戻します。ファイル。ファイルが存在しない場合は、ディレクトリのリストまたは 403 Forbidden エラーが返されます。
07. error_pageコマンド
error_page ディレクティブは、リクエストの処理中にエラーが発生したときのサーバーの動作を定義します。このディレクティブは、エラー ページを指定された URL またはローカル ファイルにリダイレクトするか、指定された HTTP ステータス コードを返すことができます。error_page ディレクティブの構文は次のとおりです。
error_page code ... [=[response]] uri;
このうち、コードは HTTP ステータス コードであり、単一のステータス コードまたはスペースで区切られた複数のステータス コードを指定できます。file または uri は、表示するエラー ページのファイル パスまたは URL です。
# 将 404 错误页面重定向到 /404.html 页面
error_page 404 /404.html;
# 把所有 404、500、502、503 和 504 错误页面重定向到 /error.html 页面。
error_page 404 500 502 503 504 /error.html;
# 把所有 404 错误页面重定向到一个 403 状态码。
error_page 404 =403;
① 特定のジャンプアドレスを指定できます。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server{
listen 8080;
server_name localhost;
error_page 404 http://www.baidu.com;
}
}
クライアントが http://192.168.38.33:8080/a をリクエストすると、http://www.baidu.com にジャンプします
② リダイレクトアドレスを指定できます。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
server_name localhost;
# 将 404 状态码到 /50x.html 页面
error_page 404 /50x.html;
# 把所有 500 502 503 504 状态码重定向到 /50x.html 页面。
error_page 500 502 503 504 /50x.html;
error_page 404 =200 /50x.html;
location =/50x.html{
root html;
}
}
}
クライアントアクセス http://192.168.38.33:8080/a
③オプション=[response]
機能はステータスコードの変更です。
[root@192 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 8080;
server_name localhost;
# 当返回404找不到对应的资源的时候,在浏览器上可以看到,最终返回的状态码是200
# 这块需要注意下,编写error_page后面的内容,404后面需要加空格,200前面不能加空格
error_page 404 =200 /50x.html;
location =/50x.html{
root html;
}
}
}