天天看點

Nginx靜态資源配置Nginx配置成系統服務靜态資源配置指令靜态資源優化配置

Nginx配置成系統服務

把Nginx應用服務設定成為系統服務,友善對Nginx服務的啟動和停止等相關操作,具體實作步驟:

  1. /usr/lib/systemd/system

    目錄下添加nginx.service,内容如下:
[Unit]
# Unit表明該服務的描述,類型描述
Description=nginx web service
Documentation=http://nginx.org/en/docs/
# 要求在network服務啟動後再啟動
After=network.target

[Service]
# 運作模式 simple|forking|oneshot|dbus|notify
Type=forking
# 存放PID檔案的位置
PIDFile=/usr/local/nginx/logs/nginx.pid
# ExecStart被執行之前被執行
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 服務運作的具體執行指令
ExecStart=/usr/local/nginx/sbin/nginx
# 服務重新開機的執行指令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 服務停止的執行指令
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=default.target
           
  1. 添加完成後如果權限有問題需要進行權限設定

chmod 755 /usr/lib/systemd/system/nginx.service

  1. 使用系統指令來操作Nginx服務.

啟動: systemctl start nginx

停止: systemctl stop nginx

重新開機: systemctl restart nginx

重新加載配置檔案: systemctl reload nginx

檢視nginx狀态: systemctl status nginx

開機啟動: systemctl enable nginx

靜态資源配置指令

設定請求資源的目錄

  1. root:設定請求的根目錄

    | 文法 | root path; |

    | — | — |

    | 預設值 | root html; |

    | 位置 | http、server、location |

path為Nginx伺服器接收到請求以後查找資源的根目錄路徑。

  1. alias:用來更改location的URI

    | 文法 | alias path; |

    | — | — |

    | 預設值 | — |

    | 位置 | location |

path為修改後的根路徑。

案例:

/usr/local/nginx/html

目錄下建立一個 images目錄,并在目錄下放入一張圖檔

1.jpg

圖檔

../html/

├── 50x.html

├── html

│ ├── 50x.html

│ └── index.html

├── images

│ └──

1.jpg

└── index.html

server {
    listen 8070;
    server_name 127.0.0.1;
    location /images {
        root /home/nginx/html;
    }
}
# 通路圖檔的路徑為:http://172.41.100.15:8070/images/1.jpg
server {
  listen 8071;
  server_name localhost;
  location /images {
    #把root改為alias
    alias /home/nginx/html;
  }
	# 正确配置
  location /imagess {
    alias /home/nginx/html/images;
  }
}
# 通路http://172.41.100.15:8071/images/1.jpg 報404
# 正确通路圖檔的路徑為:http://172.41.100.15:8071/imagess/1.jpg
           
  • root與alias的差別

root的處理結果是: root路徑+location路徑

/usr/local/nginx/html/images/1.jpg

alias的處理結果是: 使用alias路徑替換location路徑

/usr/local/nginx/html/1.jpg

如果location路徑是以/結尾,則alias也必須是以/結尾,root沒有要求

alias是一個目錄别名的定義,root則是最上層目錄的含義。

例如:

location /imagess/ {
    alias /home/nginx/html/images;
}
# 通路就會出問題,檢視錯誤日志還是路徑不對,是以需要把alias後面加上 /
2021/08/16 16:14:14 [error] 27736#0: *57 open() "/home/nginx/html/images1.jpg" failed (2: No such file or directory), 
client: 172.41.100.13, server: localhost, request: "GET /images1/1.jpg HTTP/1.1", host: "172.41.100.15:8071"
           

index指令

index:設定網站的預設首頁

文法 index file …;
預設值 index index.html;
位置 http、server、location

index後面可以跟多個設定,如果通路的時候沒有指定具體通路的資源,則會依次進行查找,找到第一個為止。

案例:

location / {
    root /home/nginx/html;
    index index.html index.htm;
}
           

通路該location的時候,可以通過 http://ip:port/,位址後面如果不添加任何内容,則預設依次通路index.html和index.htm,找到第一個來進行傳回

error_page指令

error_page:設定網站的錯誤頁面

文法 error_page code … [=[response]] uri;
預設值
位置 http、server、location…

當出現對應的響應code後,如何來處理。

案例:

# 指定具體跳轉的位址
server {
	error_page 404 http://www.baidu.com;
}

 # 指定重定向位址
server{
	error_page 404 /50x.html;
	error_page 500 502 503 504 /50x.html;
	location =/50x.html{
		root html;
	}
}

# location的@ 完成錯誤資訊展示
server{
	error_page 404 @jump_to_error;
	location @jump_to_error {
		default_type text/plain;
		return 404 'Not Found Page...';
	}
}

# =[response]的作用是用來将相應代碼更改為另外一個
server{
	error_page 404 =200 /50x.html;
	location =/50x.html{
		root html;
	}
}
#當傳回404找不到對應的資源的時候,在浏覽器上可以看到,最終傳回的狀态碼是200
#編寫error_page後面的内容,404後面需要加空格,200前面不能加空格
           

靜态資源優化配置

Nginx對靜态資源從三個屬性配置進行優化:

sendfile

on

tcp_nopush

on

tcp_nodeplay

on

  • sendfile:用來開啟高效的檔案傳輸模式。

    | 文法 | sendfile on |off; |

    | — | — |

    | 預設值 | sendfile off; |

    | 位置 | http、server、location… |

請求靜态資源的過程:

  1. 用戶端通過網絡接口向服務端發送請求.
  2. 作業系統将這些用戶端的請求傳遞給伺服器端應用程式.
  3. 伺服器端應用程式會處理這些請求.
  4. 請求處理完成以後,作業系統還需要将處理得到的結果通過網絡擴充卡傳遞回去。
Nginx靜态資源配置Nginx配置成系統服務靜态資源配置指令靜态資源優化配置

例如:

server {
	listen 80;
	server_name localhost;
	location / {
		root html;
		index index.html;
	}
}
# 在html目錄下有一個welcome.html頁面,通路位址
http://172.41.100.15/welcome.html
           

未使用

sendfile

流程:

Nginx靜态資源配置Nginx配置成系統服務靜态資源配置指令靜态資源優化配置

使用

sendfile

流程:

Nginx靜态資源配置Nginx配置成系統服務靜态資源配置指令靜态資源優化配置
  • tcp_nopush:該指令必須在sendfile打開的狀态下才會生效,主要是用來提升網絡包的傳輸’效率’,相當于開啟緩沖區

    | 文法 | tcp_nopush on|off; |

    | — | — |

    | 預設值 | tcp_nopush off; |

    | 位置 | http、server、location |

  • tcp_nodelay:該指令必須在keep-alive連接配接開啟的情況下才生效,來提高網絡包傳輸的’實時性’,即有資料立刻發送

    | 文法 | tcp_nodelay on|off; |

    | — | — |

    | 預設值 | tcp_nodelay on; |

    | 位置 | http、server、location |

sendfile

可以開啟高效的檔案傳輸模式,

tcp_nopush

開啟可以確定在發送到用戶端之前資料緩沖區已經充分“填滿”, 這大大減少了網絡開銷,并加快了檔案發送的速度。 然後,當它到達最後一個可能因為沒有“填滿”而暫停的資料包時,Nginx會忽略

tcp_nopush

參數, 然後,

tcp_nodelay

強制套接字發送資料。由此可知,

tcp_nopush

可以與

tcp_nodelay

一起設定,它比單獨配置

tcp_nodelay

具有更強的性能。

**

繼續閱讀