[root@web01 conf.d]# cd ~
[root@web01 ~]# cd /etc/nginx/conf.d/
一、#配置nginx(server1.conf)
[root@web01 conf.d]# vim server1.conf
server {
listen 80;
server_name localhost server1.com;
location / {
root /code/test1;
index index.html;
}
}
#配置nginx(server2.conf)
[root@web01 conf.d]# vim server2.conf
server {
listen 80;
server_name localhost server2.com;
location / {
root /code/test2;
index index.html;
}
}
#配置nginx(server3.conf)
[root@web01 conf.d]# vim server3.conf
server {
listen 80;
server_name localhost server3.com;
location / {
root /code/test3;
index index.html;
}
}
二、#準備站點目錄
[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
三、#檢查語法提示衝突,忽略並重啓
[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: [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
/etc/nginx/conf.d
中,把其餘conf
文件都打包一下,只留server1.conf
、server2.conf
、server3.conf
。四、#根據ip訪問
#1. 用戶第一次訪問,讀取server1.conf配置返回結果
[root@web01 code]# curl 10.0.0.7
test1
五、#此時將server1.conf修改成server4.conf重啓nginx
[root@web01 code]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# mv server1.conf server4.conf
[root@lb01 conf.d]# nginx -s reload
六、#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
修改內容以下:10.0.0.7 server1.com server2.com server3.com
七、#檢查並重啓nginx
[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: [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
八、#再次訪問時,讀取server2.conf配置返回結果
[root@web01 conf.d]# curl 10.0.0.7
test2
測試訪問效果,打開瀏覽器,輸入server1.comhtml
Server_name
優先級總結server_name
進行匹配,來決定用哪個server標籤來完成處理這個請求,有可能一個Host與多個server中的server_name
都匹配,這個時候就會根據匹配優先級來選擇實際處理的server。優先級匹配結果以下:1.首先選擇全部的字符串徹底匹配的server_name。(徹底匹配)java
2.選擇通配符在前面的server_name,如.haoda.com www.haoda.comlinux
3.選擇通配符在後面的server_name,如bgx.* haoda.com haoda.cnnginx
4.最後選擇使用正則表達式匹配的server_nameweb
5.若是所有都沒有匹配到,那麼將選擇在listen配置項後加入[default_server]的server塊正則表達式
6.若是沒寫,那麼就找到匹配listen端口的第一個Server塊的配置文件vim
server_name
狀況下,配置文件排序優先使用則會被調用,因此建議配置相同端口,不一樣域名,這樣不會出現域名訪問衝突。
#配置nginx
[root@web01 conf.d]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim server4.conf
server {
listen 80 default_server; #默認優先返回;
server_name _; #空主機頭或者IP;
return 500; #直接返回500錯誤;
}
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
註釋內容以下:10.0.0.7 server1.com server2.com server3.com
#檢查並重啓nginx
[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
測試訪問效果,打開瀏覽器,輸入10.0.0.7
#配置nginx
[root@web01 conf.d]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim server4.conf
server {
listen 80 default_server;
server_name _;
return 302 http://server1.com;
}
[root@web01 conf.d]# mkdir /code/test1
[root@web01 conf.d]# echo test1 > /code/test1/index.html
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 server1.com
[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
#配置nginx
[root@web01 conf.d]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim img.conf
server{
listen 80;
server_name img.haoda.com;
location /images {
root /code/img/;
}
}
#在code目錄下建立img目錄
[root@web01 code]# cd ~
[root@web01 ~]# mkdir -p /code/img/
#在img目錄下上傳圖片
[root@web01 code]# cd img
[root@web01 img]# rz -E
rz waiting to receive.
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 img.haoda.com
#檢查並重啓nginx
[root@web01 img]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 img]# nginx -s reload
解決方案:在/code/img目錄下建立images目錄,將img目錄下的圖片移至imageswindows
[root@web01 img]# mkdir -p /code/img/images/
[root@web01 images]# rz -E
rz waiting to receive.
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 img.haoda.com
#檢查並重啓nginx
[root@web01 img]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 img]# nginx -s reload
#使用alias,即/images等價於/code/img
server{
listen 80;
server_name img.haoda.com;
location /images {
alias /code/img;
}
}
#在/code/img目錄下存在圖片
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 img.haoda.com
#檢查並重啓nginx
[root@web01 img]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 img]# nginx -s reload
try_file
路徑匹配,Nginx會按順序檢查文件及目錄是否存在(根據 root 和 alias 指令設置的參數構造完整的文件路徑),並用找到的第一個文件提供服務。在元素名後面添加斜槓 / 表示這個是目錄。若是文件和目錄都不存在,Nginx會執行內部重定向,跳轉到命令的最後一個 uri 參數定義的 URI 中。#1.配置nginx
[root@web01 img]# cd /etc/nginx/conf.d/
[root@web01 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@web01 conf.d]# echo try111 > /code/index.html
[root@lb01 conf.d]# echo '404 404 404' > /code/404.html
#3.windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 try.haoda.com
#4.檢查並重啓nginx
[root@web01 img]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 img]# nginx -s reload
$uri
取得是域名後面咱們寫的內容,因爲輸入信息時域名後面沒有內容,它找不到,因此返回$uri
中後面的內容,即404.html)
#此時修改配置文件
server {
listen 80;
server_name try.haoda.com;
root /code;
index index.html;
location / {
try_files $uri $uri/ /404.html;
}
}
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 try.haoda.com
#檢查並重啓nginx
[root@web01 img]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 img]# nginx -s reload
$uri
是域名後面的內容,咱們在輸入域名信息時,域名後面沒有寫任何內容,所以沒有匹配到第一個$uri
,因而第二個$uri
就是「空/」,他訪問的即是「空/」,也就是咱們在瀏覽器中輸入try.haoda.com/,跳轉index頁面,進的是根,就是root,即匹配到/code/index.html)
#1. 配置nginx
[root@web01 conf.d]# vim 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. 在web02上配置後端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.檢查並重啓nginx
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 code]# nginx -s reload
#5. 測試訪問
[root@lb01 code]# curl http://try.haoda.com/index.html
i am tomcat
#配置nginx
[root@web01 code]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim error.conf
server {
listen 80;
server_name www.haoda.com;
root /code;
location / {
index index.html;
error_page 404 http://www.baidu.com;
}
}
恢復/code目錄下index.html,使得該文件沒被壓縮
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 www.haoda.com
#檢查並重啓nginx
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 code]# nginx -s reload
#配置nginx
[root@web01 code]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim error.conf
server {
listen 80;
server_name error.haoda.com;
root /code;
location / {
index index.html;
}
error_page 404 403 /404.jpg;
}
#在code目錄下上傳404.jpg
[root@web01 conf.d]# cd /code
[root@web01 code]# ll
total 80212
-rw-r--r-- 1 root root 12 Sep 2 19:11 404.html
-rw-r--r-- 1 root root 29239 Sep 2 20:30 404.jpg
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 error.haoda.com
#檢查並重啓nginx
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 code]# nginx -s reload
#配置nginx
[root@web01 code]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim error.conf
server {
listen 80;
server_name error.haoda.com;
root /code;
location / {
index index.html;
}
error_page 403 404 /404.html;
location = /404.html {
root /code;
index index.html;
}
}
#windows鍵+R,輸入drivers,找到etc目錄,進入etc目錄,找到host域名解析文件,
輸入內容以下:10.0.0.7 error.haoda.com
[root@web01 code]# cat 404.html
404 404 404
[root@web01 code]# cat index.html
try111
#檢查並重啓nginx
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 code]# nginx -s reload