image.png
image.png
linux系統(tǒng)訪問和瀏覽器訪問報(bào)404
[root@localhost opt]#cd /apps/nginx/conf/server
[root@localhost server]#vim www_magedu_net.conf
location = /favicon.ico {
root /data/nginx/html/pc/images; 添加圖標(biāo)的路徑
}
保存圖片
[root@localhost server]#mkdir /data/nginx/html/pc/images
[root@localhost server]#cd /data/nginx/html/pc/images
[root@nginxserver images]#ll
total 4
-rw-r--r-- 1 root root 1150 Jun 1 01:07 favicon.ico 把保存到桌面的圖片贾惦,上傳到服務(wù)器里
[root@nginxserver images]#nginx -s reload
這樣就不會(huì)報(bào)錯(cuò)了
我們一般看400碾盟,403,500,50這樣的錯(cuò)誤
404一般是圖片找不到
[root@nginxserver conf]#cd /apps/nginx/conf/
[root@nginxserver conf]#vim nginx.conf
# server_tokens off; 把這行注釋掉
這樣就是顯示nginx的版本了
[root@nginxserver conf]#vim nginx.conf
server_tokens off; 把這行添加上就是默認(rèn)是nginx的名稱顯示版本了
[root@nginxserver nginx-1.14.2]#vim /data/nginx-1.14.2/src/http/ngx_http_header_filter_module.c +49
static u_char ngx_http_server_string[] = "Server: linux36" CRLF; 修改他的server-name服務(wù)器的版本號(hào)
[root@nginxserver nginx-1.14.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module --add-module=/root/echo-nginx-module 重新編譯一下
[root@nginxserver nginx-1.14.2]#make
[root@nginxserver nginx-1.14.2]#make install
[root@nginxserver nginx-1.14.2]#nginx
一般情況下都需要改掉這個(gè)名稱舀患,這樣相對(duì)安全戈抄,php靠柑,java服務(wù)都改掉
查看他的服務(wù)器的名稱已經(jīng)修改了
[root@nginxserver conf]#vim server/www_magedu_net.conf
default_type text/html; 定義一個(gè)類型
if ( $scheme = http ){ 如果值等于http就執(zhí)行下邊的操作篓跛,如果用戶訪問的是http就執(zhí)行下邊的
echo "if-----> $scheme";
}
[root@nginxserver conf]#nginx -s reload
這個(gè)時(shí)候就是匹配成功了
[root@nginxserver conf]#vim server/www_magedu_net.conf
default_type text/html;
if ( $scheme != http ){ 如果訪問的不是http就執(zhí)行下邊的
echo "if-----> $scheme";
這個(gè)時(shí)候就不匹配了
如果訪問的是https就不執(zhí)行這個(gè)操作了
[root@nginxserver conf]#vim server/www_magedu_net.conf
location / {
root /data/nginx/html/pc;
if (-f $request_filename) { 判斷文件是否存在
echo "file is exist";
}
}
[root@nginxserver conf]#nginx -s reload
顯示這個(gè)頁(yè)面是存在的
訪問錯(cuò)誤的頁(yè)面
[root@nginxserver conf]#vim server/www_magedu_net.conf
if (!-f $request_filename) { 判斷這個(gè)文件是否不存在
echo "file is exist";
}
[root@nginxserver conf]#nginx -s reload
echo "alias nginx-r='nginx -s reload'" >>/etc/profile 定義一個(gè)nginx重新加載的命令
[root@nginxserver conf]#vi server/www_magedu_net.conf
if (!-f $request_filename) {
echo "file is not exist"; 文件不存在就打印這行
}
[root@nginxserver conf]#echo "alias cdnc='cd /apps/nginx/conf/'" >>/etc/profile
文件不存在就出現(xiàn)這個(gè), 他訪問的路徑是這里root /data/nginx/html/pc;
[root@nginxserver server]#vim www_magedu_net.conf
set $name magedu;
echo $name;
set $my_port $server_port;
echo $my_port;
[root@nginxserver server]#nginx-r
取出來的是二個(gè)值
[root@nginxserver server]#vim www_magedu_net.conf
set $name magedu;
echo $name;
break; 加上brak后邊的命令就不在執(zhí)行了
set $my_port $server_port;
echo $my_port;
[root@nginxserver server]#nginx-r
后邊的打印80端口就沒有執(zhí)行
[root@nginxserver server]#vim www_magedu_net.conf
root /data/nginx/html/pc;
if ( $scheme = http ) { 如果訪問的內(nèi)容是http就return 出現(xiàn)一個(gè)狀態(tài)碼
return 666;
}
出現(xiàn)了一個(gè)狀態(tài)碼
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
return 301 https://www.magedu.net; 301狀態(tài)碼 和訪問https的網(wǎng)站
}
[root@nginxserver server]#nginx-r
第一次訪問的是http
返回的是一個(gè)狀態(tài)碼還有跳轉(zhuǎn)到https的網(wǎng)站了
從http跳轉(zhuǎn)到https的網(wǎng)站
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
return 666 "not allow http"; 不允許http顯示
# return 301 https://www.magedu.net;
}
[root@nginxserver server]#nginx-r
不允許http顯示
image.png
[root@client ~]#curl http://www.magedu.net/ 使用linuxcurl命令鹰溜,直接返回不允許使用http訪問
not allow http[root@client ~]#
到了服務(wù)器上都是rui
用戶訪問的url
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
rewrite / https://www.magedu.net redirect;
}
[root@nginxserver server]#nginx-r
第一次訪問是302
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
# rewrite / https://www.magedu.net redirect;
rewrite / https://www.magedu.net permanent; 更改成永久重定向 這個(gè)是301
}
[root@nginxserver server]#nginx-r
通過hhttp訪問已經(jīng)有緩存了
301會(huì)緩存,302不會(huì)緩存是臨時(shí)重定向
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
# rewrite / https://www.magedu.net redirect;
rewrite / http://www.magedu.com permanent; 設(shè)置當(dāng)訪問www.magedu.net就會(huì)重定向到 http://www.magedu.com這個(gè)網(wǎng)站
}
[root@nginxserver server]#nginx-r
好處緩存會(huì)加速丁恭,壞處就是和最新的數(shù)據(jù)不一致
明明網(wǎng)站更新了曹动,但是由于緩存用戶沒有看到新的頁(yè)面
當(dāng)瀏覽器收到請(qǐng)求之后就會(huì)
[root@nginxserver server]#vim www_magedu_net.conf
# if ( $scheme = http ) {
rewrite / https://www.magedu.net redirect; 只要訪問http就定義到這個(gè)網(wǎng)站實(shí)現(xiàn)一直循環(huán),這個(gè)請(qǐng)求不正常牲览,沒有判斷什么條件下墓陈,這樣就導(dǎo)致死循環(huán)
# rewrite / http://www.magedu.com permanent;
#}
[root@nginxserver server]#nginx-r
image.png
[root@nginxserver server]#vim www_magedu_net.conf
# if ( $scheme = http ) {
rewrite / http://www.magedu.net redirect; 去掉s
# rewrite / http://www.magedu.com permanent;
#}
[root@nginxserver server]#nginx-r
出現(xiàn)重定向次數(shù)過多,這個(gè)就是rewrite規(guī)則寫的有問題第献,所以在寫rewrite規(guī)則之前一定要經(jīng)過測(cè)試贡必,一定要寫上在什么樣的條件下才做rewrite
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
rewrite / https://www.magedu.net redirect;
# rewrite / http://www.magedu.com permanent;
#}
[root@nginxserver server]#nginx-r
訪問成功
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
rewrite / https://www.magedu.net last;
# rewrite / http://www.magedu.com permanent;
#}
[root@nginxserver server]#nginx-r
利用nginx的rewrite的指令,可以實(shí)現(xiàn)url的重新跳轉(zhuǎn)庸毫,rewrtie有四種不同的flag赊级,分別是redirect(臨時(shí)重定向)、
permanent(永久重定向)岔绸、break和last理逊。其中前兩種是跳轉(zhuǎn)型的flag,后兩種是代理型盒揉,跳轉(zhuǎn)型是指有客戶端瀏覽
器重新對(duì)新地址進(jìn)行請(qǐng)求晋被,代理型是在WEB服務(wù)器內(nèi)部實(shí)現(xiàn)跳轉(zhuǎn)的。
[root@nginxserver server]#vim www_magedu_net.conf
if ( $scheme = http ) {
rewrite / https://www.magedu.net break;
# rewrite / http://www.magedu.com permanent;
#}
[root@nginxserver server]#nginx-r
image.png
[root@nginxserver server]#vim www_magedu_net.conf
location /break {
rewrite ^/break/(.*) /test$1 break;
return 666 "break";
}
location /last {
rewrite ^/last/(.*) /test$1 last;
return 888 "last";
}
location /test {
return 999 "test";
}
[root@client ~]#curl -L http://www.magedu.net/break break不會(huì)跟隨跳轉(zhuǎn)的break還是break
break[root@client ~]#
[root@client ~]#curl -L http://www.magedu.net/last last會(huì)顯示last的值刚盈,由于標(biāo)記位是last 羡洛,產(chǎn)生的值也是last
last[root@client ~]#
[root@client ~]# curl -I http://www.magedu.net/lastst
HTTP/1.1 888
Server: linux36
Date: Sat, 01 Jun 2019 09:15:27 GMT
Content-Type: application/octet-stream
Content-Length: 4
Connection: keep-alive
Keep-Alive: timeout=60
[root@client ~]# curl -L -I http://www.magedu.net/last/
HTTP/1.1 999
Server: linux36
Date: Sat, 01 Jun 2019 09:16:55 GMT
Content-Type: application/octet-stream
Content-Length: 4
Connection: keep-alive
Keep-Alive: timeout=60
[root@client ~]# curl -L -I http://www.magedu.net/last/index.html
HTTP/1.1 999
Server: linux36
Date: Sat, 01 Jun 2019 09:17:47 GMT
Content-Type: text/html
Content-Length: 4
Connection: keep-alive
Keep-Alive: timeout=60
[root@client ~]#curl -L -I http://www.magedu.net/break/index.html
HTTP/1.1 404 Not Found
Server: linux36
Date: Sat, 01 Jun 2019 09:19:29 GMT
Content-Type: text/html
Content-Length: 14
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "5cef66b7-e"
[root@client ~]#curl -L -I http://www.magedu.net/last/index.html
HTTP/1.1 999 訪問last才會(huì)調(diào)到test里邊
Server: linux36
Date: Sat, 01 Jun 2019 09:20:13 GMT
Content-Type: text/html
Content-Length: 4
Connection: keep-alive
Keep-Alive: timeout=60
[root@nginxserver server]#vim www_magedu_net.conf
location /test {
# return 999 "test";
index index.html;
root /data/nginx; 生成路徑 從這個(gè)文件里找
}
[root@nginxserver server]#mkdir /data/nginx/test
[root@nginxserver server]#vim /data/nginx/test/index.html
test
[root@nginxserver server]#nginx-r
break
找不到報(bào)錯(cuò)誤頁(yè)面
last找不到這個(gè)頁(yè)面
[root@client ~]#curl -L -I http://www.magedu.net/last/index.html
HTTP/1.1 404 Not Found
Server: linux36
Date: Sat, 01 Jun 2019 09:33:43 GMT
Content-Type: text/html
Content-Length: 14
Connection: keep-alive
Keep-Alive: timeout=60
ETag: "5cef66b7-e"
[root@nginxserver conf]#vi server/www_magedu_net.conf
location /test {
return 999 "test";先讓他跳到test里 999就是在test里面做返回了
# index index.html;
# root /data/nginx;
}
}
[root@nginxserver conf]#nginx-r
[root@client ~]#curl -L -I http://www.magedu.net/last/index.html
HTTP/1.1 999
Server: linux36
Date: Sat, 01 Jun 2019 09:36:11 GMT
Content-Type: text/html test里retoon可以的
Content-Length: 4
Connection: keep-alive
Keep-Alive: timeout=60

break僅限當(dāng)前l(fā)ocation里去做匹配
location /break {
rewrite ^/break/(.*) /test/$1 break;
return 666 "break";
}
location /last {
rewrite ^/last/(.*) /test/$1 last;
return 888 "last";
}
location /test {
return 999 "test";
# index index.html;
# root /data/nginx;
[root@nginxserver conf]#vi server/www_magedu_net.conf
rewrite ^/break/(.*)/ /test/$1 break;
return 666 "break"; btrak會(huì)在當(dāng)前的location里判斷,這里邊匹配成功之后或者失敗之后藕漱,就不在匹配了欲侮,并且把匹配后的結(jié)果發(fā)給客戶端
}
location /last {
rewrite ^/last/(.*) /test/$1 last; last也是在當(dāng)前的location里做匹配,他會(huì)在匹配之后的規(guī)則然后在下一個(gè)location在進(jìn)行匹配肋联,直到匹配完成威蕉,之后的location為止,把新生成的location
return 888 "last";
}
location /test {
# return 999 "test";
index index.html;
root /data/nginx;
}
}
[root@client ~]#curl -L http://www.magedu.net/break/index.html
break 第一次訪問的break
root@client ~]#curl -L http://www.magedu.net/last/index.html
test 訪問last是出現(xiàn)的test
[root@nginxserver conf]#vi server/www_magedu_net.conf
location /break {
rewrite ^/break/(.*)/ /test/$1 break;
return 666 "break";
root /data/nginx;
index index.html;
}
location /last {
rewrite ^/last/(.*) /test/$1 last;
root /data/nginx;
index index.html;
}
location /test {
# return 999 "test";
index index.html;
root /data/nginx;
}
}
[root@nginxserver conf]#mkdir /data/nginx/break
[root@nginxserver conf]#mkdir /data/nginx/last
[root@nginxserver conf]#vim /data/nginx/last/index.html
last
[root@nginxserver conf]#vim /data/nginx/break/index.html
break
[root@nginxserver conf]#nginx-r