文章目錄
- nginx初始化
- docker拉取nginx鏡像
- 實體化nginx鏡像
- 啟動nginx
- nginx常用命令
- 查看nginx版本號
- 關閉nginx
- 啟動nginx
- 重新加載nginx
- nginx組態檔
- 全域塊
- events塊
- http塊
- http全域塊
- server塊
- nginx配置反向代理
- 簡單代理
- 復雜代理
- nginx配置負載均衡
- 輪詢
- weight
- ip_hash
- fair(第三方)
- nginx配置動靜分離
nginx初始化
docker拉取nginx鏡像

[root@Dragon ~]# dk pull nginx
實體化nginx鏡像
[root@Dragon ~]# dk run -it --name nginx1 -p 80:80 nginx /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
有一個警告意思是ipv4的轉發未開啟先不管
啟動nginx
找一下啟動檔案
root@65493cd4789a:/usr/local/sbin# find / -name nginx
/etc/default/nginx
/etc/init.d/nginx
/etc/logrotate.d/nginx
/etc/nginx
find: '/proc/1/map_files': Operation not permitted
find: '/proc/11/map_files': Operation not permitted
/usr/lib/nginx
/usr/sbin/nginx
/usr/share/doc/nginx
/usr/share/nginx
/var/cache/nginx
/var/log/nginx
/usr/sbin/nginx看起來比較像
root@65493cd4789a:/usr/local/sbin# cd /usr/sbin
root@65493cd4789a:/usr/sbin# ./nginx
2021/10/26 12:47:04 [notice] 13#13: using the "epoll" event method
2021/10/26 12:47:04 [notice] 13#13: nginx/1.21.3
2021/10/26 12:47:04 [notice] 13#13: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/10/26 12:47:04 [notice] 13#13: OS: Linux 3.10.0-1160.el7.x86_64
2021/10/26 12:47:04 [notice] 13#13: getrlimit(RLIMIT_NOFILE): 1048576:1048576
有幾行notice先不管
查看nginx組態檔
root@65493cd4789a:/usr/sbin# find / -name nginx.conf
/etc/nginx/nginx.conf
find: '/proc/1/map_files': Operation not permitted
find: '/proc/14/map_files': Operation not permitted
find: '/proc/15/map_files': Operation not permitted
find: '/proc/19/map_files': Operation not permitted
root@65493cd4789a:/usr/sbin# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
在宿主機瀏覽器順利訪問到nginx的welcome頁面
nginx常用命令
nginx命令需要在nginx的目錄下才能使用
查看nginx版本號
root@e137e425fbf3:/usr/sbin# ./nginx -v
nginx version: nginx/1.21.3
關閉nginx
root@e137e425fbf3:/usr/sbin# ./nginx -s stop
2021/10/26 13:47:45 [notice] 42#42: signal process started
啟動nginx
./nginx
重新加載nginx
可加載組態檔里的內容
./ nginx -s reload
nginx組態檔
先創建nginx的容器資料卷
在root/nginx目錄下創建www、logs、conf目錄
- conf:nginx組態檔目錄
- logs:nginx日志目錄
- www:nginx存盤網站網頁目錄
拷貝組態檔到本地
[root@Dragon ~]# dk cp nginx1:/etc/nginx/nginx.conf /root/nginx/conf
創建新的容器并將www、logs、conf目錄映射到本地
docker run -d -p 80:80 --name d_nginx -v /root/nginx/www:/usr/share/nginx/html -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /root/nginx/logs:/var/log/nginx nginx
這樣就可以在容器外編輯組態檔了
組態檔由四部分組成
全域塊
從組態檔開始到 events 塊之間的內容,主要會設定一些影響 nginx 服務器整體運行的配置指令,主要包括配
置運行 Nginx 服務器的用戶(組)、允許生成的 worker process 數, 行程 PID 存放路徑、日志存放路徑和型別以
及組態檔的引入等,
worker_processes auto;
這是 Nginx 服務器并發處理服務的關鍵配置, worker_processes 值越大,可以支持的并發處理量也越多,但是
會受到硬體、軟體等設備的制約
events塊
events 塊涉及的指令主要影響 Nginx 服務器與用戶的網路連接,常用的設定包括是否開啟對多 work process
下的網路連接進行序列化,是否允許同時接收多個網路連接,選取哪種事件驅動模型來處理連接請求,每個 word
process 可以同時支持的最大連接數等,
events {
worker_connections 1024;
}
上述例子就表示每個 work process 支持的最大連接數為 1024.
這部分的配置對 Nginx 的性能影響較大,在實際中應該靈活配置
http塊
這算是 Nginx 服務器配置中最頻繁的部分,代理、快取和日志定義等絕大多數功能和第三方模塊的配置都在這里,
需要注意的是: http 塊也可以包括 http 全域塊、 server 塊,
http全域塊
http 全域塊配置的指令包括檔案引入、 MIME-TYPE 定義、日志自定義、連接超時時間、單鏈接請求數上限等
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
server塊
在/etc/nginx/conf.d/目錄下的default.conf中
這塊和虛擬主機有密切關系,虛擬主機從用戶角度看,和一臺獨立的硬體主機是完全一樣的,該技術的產生是為了
節省互聯網服務器硬體成本,
每個 http 塊可以包括多個 server 塊,而每個 server 塊就相當于一個虛擬主機,
而每個 server 塊也分為全域 server 塊,以及可以同時包含多個 locaton 塊,
1、全域 server 塊
最常見的配置是本虛擬機主機的監聽配置和本虛擬主機的名稱或 IP 配置,
2、 location 塊
一個 server 塊可以配置多個 location 塊,
這塊的主要作用是基于 Nginx 服務器接收到的請求字串(例如 server_name/uri-string),對虛擬主機名稱
(也可以是 IP 別名)之外的字串(例如 前面的 /uri-string)進行匹配,對特定的請求進行處理,地址定向、資料緩
存和應答控制等功能,還有許多第三方模塊的配置也在這里進行,
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
nginx配置反向代理
簡單代理
拉取tomcat鏡像,映射到8080埠
[root@Dragon conf]# dk run -d -p 8080:8080 --name d_tomcat tomcat:8.0.52
我們直接在http塊中添加自定義的server塊即可,配置80埠的請求轉發到tomcat的8080埠
server{
listen 80;
charset utf-8;
server_name 192.168.152.161;
location / {
proxy_pass http://192.168.152.161:8080;
proxy_redirect default;
}
}
重啟d_nginx鏡像,再次用宿主機瀏覽器訪問虛擬機80埠,成功訪問到tomcat的welcome頁面,反向代理成功!
復雜代理
準備兩個jar程式分別在8888埠和9999埠,配置server塊讓訪問虛擬機的uri中帶有index.html的轉發到9999埠,uri中帶有cartoonIndex.html的轉發到8888埠,
server{
listen 80;
charset utf-8;
server_name 192.168.152.161;
location ~/cartoonIndex.html {
proxy_pass http://192.168.152.161:8888;
}
location ~/index.html {
proxy_pass http://192.168.152.161:9999;
proxy_redirect default;
}
這里只是一個簡單的例子,這個只轉發了兩個頁面,其余的東西沒有轉發,正常情況下應該配置路徑
,
nginx配置負載均衡
將同一個請求平均分到兩個服務器上
配置負載均衡時要把默認配置include /etc/nginx/conf.d/*.conf;洗掉
upstream myserver {
server 192.168.152.161:8888;
server 192.168.152.161:9999;
}
server{
listen 80;
charset utf-8;
server_name 192.168.152.161;
location / {
proxy_pass http://myserver;
}
}
默認負載均衡的策略時輪詢
輪詢
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器 down 掉,能自動剔除,
weight
指定輪詢幾率, weight 和訪問比率成正比,用于后端服務器性能不均的情況,
upstream myserver {
server 192.168.152.161:8888 weight=10;
server 192.168.152.161:9999 weight=15;
}
ip_hash
每個請求按訪問 ip 的 hash 結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決 session 的問題,
upstream myserver {
ip_hash;
server 192.168.152.161:8888;
server 192.168.152.161:9999;
}
fair(第三方)
按后端服務器的回應時間來分配請求,回應時間短的優先分配,
upstream server_pool{
server 192.168.152.161:8888;
server 192.168.152.161:9999;
fair;
}
nginx配置動靜分離
Nginx 動靜分離簡單來說就是把動態跟靜態請求分開,不能理解成只是單純的把動態頁面和
靜態頁面物理分離,嚴格意義上說應該是動態請求跟靜態請求分開,可以理解成使用 Nginx
處理靜態頁面, Tomcat 處理動態頁面,
動靜分離從目前實作角度來講大致分為兩種
一種是純粹把靜態檔案獨立成單獨的域名,放在獨立的服務器上,也是目前主流推崇的方案;
另外一種方法就是動態跟靜態檔案混合在一起發布,通過 nginx 來分開,
通過 location 指定不同的后綴名實作不同的請求轉發,通過 expires 引數設定,可以使
瀏覽器快取過期時間,減少與服務器之前的請求和流量,具體 Expires 定義:是給一個資
源設定一個過期時間,也就是說無需去服務端驗證,直接通過瀏覽器自身確認是否過期即可,
所以不會產生額外的流量,此種方法非常適合不經常變動的資源,(如果經常更新的檔案,
不建議使用 Expires 來快取),我這里設定 3d,表示在這 3 天之內訪問這個 URL,發送
一個請求,比對服務器該檔案最后更新時間沒有變化,則不會從服務器抓取,回傳狀態碼
304,如果有修改,則直接從服務器重新下載,回傳狀態碼 200,
server{
listen 80;
charset utf-8;
server_name 192.168.152.161;
location /www/ {
root /data;
index index.html index.htm;
autoindex on;
}
location /image/ {
root /data/;
autoindex on;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353328.html
標籤:其他
下一篇:“21天好習慣”第一期——16
