nginx 日誌文件詳解**
nginx 日誌文件分爲 log_format 和 access_log 兩部分
log_format 定義記錄的格式,其語法格式爲
log_format 樣式名稱 樣式詳情
配置文件中默認有
log_format main 'remote_addr - remote_user [time_local] "request" '
'status body_bytes_sent "$http_referer" '
'"http_user_agent" "http_x_forwarded_for"';
使用 limit_rate 限制客戶端傳輸數據的速度**
1、編輯/etc/nginx/nginx.conf
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k; #對每個連接的限速爲2k/s
}
1、編輯/etc/nginx/nginx.conf
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k; #對每個連接的限速爲2k/s
}
修改完配置文件,需要重啓服務(重新加載服務)
注意要點:
什麼是虛擬主機?
虛擬主機是一種特殊的軟硬件技術,它可以將網絡上的每一臺計算機分成多個虛擬主機,每個虛擬主機可以獨立對外提供www服務,這樣就可以實現一臺主機對外提供多個web服務,每個虛擬主機之間是獨立的,互不影響。
nginx可以實現虛擬主機的配置,nginx支持三種類型的虛擬主機配置。
1、基於域名的虛擬主機 (server_name來區分虛擬主機——應用:外部網站)
2、基於ip的虛擬主機, (一塊主機綁定多個ip地址)
3、基於端口的虛擬主機 (端口來區分虛擬主機——應用:公司內部網站,外部網站的管理後臺)
這個主要配置 server 模塊就可以了,可以把主配置文件的server註釋了,然後在重新在/etc/nginx/conf.d/ 下新建配置文件 必須以.conf結尾 寫上一下內容即可
主配置文件必須有 include /etc/nginx/conf.d/*.conf (在http 模塊下面)
1、 基於域名的虛擬主機
1、配置通過域名區分的虛擬機
server {
listen 80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
}
server {
listen 80;
server_name web.1000phone.com;
location / {
root /1000phone/html;
index index.html index.htm;
}
}
[root@localhost nginx]# vim /var/www/nginx/index.html
hello tianyun
2、 爲域名爲 web.1000phone.com 的虛擬機,創建 index 文件
[root@localhost ~]# mkdir -p /1000phone/html
[root@localhost ~]# vim /1000phone/html/index.html
this is my 1000phone
3、重新加載配置文件
# 如果編譯安裝的執行
[root@nginx]# /usr/local/nginx/sbin/nginx -s reload
# 如果 yum 安裝的執行
[root@nginx]# nginx -s reload
4、客戶端配置解析
在 C:\Windows\System32\drivers\etc\hosts 文件中添加兩行(linux:/etc/hosts)
10.0.105.199 web.testpm.com
10.0.105.199 web.1000phone.com
5、 測試訪問
瀏覽器輸入:http://web.testpm.com/
瀏覽器輸入:http://web.1000phone.com/
2、 基於ip的虛擬主機
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:17:f1:af brd ff:ff:ff:ff:ff:ff
inet 10.0.105.199/24 brd 10.0.105.255 scope global dynamic ens33
valid_lft 81438sec preferred_lft 81438sec
inet6 fe80::9d26:f3f0:db9c:c9be/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24 #增加虛擬ip
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.105.199 netmask 255.255.255.0 broadcast 10.0.105.255
inet6 fe80::9d26:f3f0:db9c:c9be prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:17:f1:af txqueuelen 1000 (Ethernet)
RX packets 9844 bytes 1052722 (1.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5567 bytes 886269 (865.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.105.201 netmask 255.255.255.0 broadcast 10.0.105.255
ether 00:0c:29:17:f1:af txqueuelen 1000 (Ethernet)
2、配置通過ip區分的虛擬機
server {
listen 80;
server_name 10.0.105.199;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
server {
listen 80;
server_name 10.0.105.201;
location / {
root /1000phone/html/;
index index.html index.htm;
}
}
3、重新加載配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
4、 測試訪問
瀏覽器輸入:http://10.0.105.199
瀏覽器輸入:http://10.0.105.201
5、補充
– 刪除綁定的臨時ip
[root@localhost ~]# ifconfig ens33:1 10.0.105.201/24 down
重啓一下nginx
[root@localhost ~]# systemctl restart nginx
3、 基於端口的虛擬主機
server {
listen 80;
server_name web.testpm.com;
location / {
root /var/www/nginx/;
index index.html index.htm;
limit_rate 2k;
}
server {
listen 8080;
server_name web.testpm.com;
location / {
root /1000phone/html/;
index index.html index.htm;
}
}
重新加載配置文件:
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
測試訪問:
瀏覽器輸入:http://web.testpm.com/
瀏覽器輸入:http://web.1000phone.com:8080