Nginx基礎
1、nginx啟動的2種方式
- 這2種方式不能混用,只能用一種
## 第一種:(適合源碼編譯安裝)
nginx 啟動
nginx -s stop 停止
nginx -s reload | restart
## 第二種:(適合yum安裝)
systemctl start nginx 啟動
systemctl stop nginx 停止
systemctl restart nginx 重啟
2、nginx的配置文件
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types; # 包含資源類型文件
default_type application/octet-stream; # 默認以下載方式傳輸給瀏覽器(前提是該資源在mime.types中無法找到)
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; # 以main格式記錄日志到該路勁下
sendfile on; # 高效文件傳輸方式
#tcp_nopush on;
keepalive_timeout 65; # 長連接的超時時間
#gzip on; # 是否開啟壓縮功能
include /etc/nginx/conf.d/*.conf; # nginx站點目錄
}
3、站點目錄配置解析
server { # 定義一個網站
listen 80; # 監聽端口
server_name localhost; # 域名
#charset koi8-r; # 字符集
#access_log /var/log/nginx/host.access.log main; # 日志(若沒配置,則http模塊中的日志配置生效)
location / {
root /usr/share/nginx/html;
# 代碼的主文件位置
# / 代表站點路勁,即 / = /usr/share/nginx/html,若根為 /test,則站點目錄為 /usr/share/nginx/html/test/
index index.html index.htm; # 服務端默認返回給用戶的文件
# 用戶最終請求到的路徑為:/usr/share/nginx/html/index.html
}
http server location 擴展了解項
http{} 層下允許有多個Server{}層,一個Server{}層下又允許有多個Location
http{} 標簽主要用來解決用戶的請求與響應
Server{} 標簽主要用來響應具體的某一個網站
Location{} 標簽主要用於匹配網站具體的URL路徑
4、nginx搭建靜態資源web服務器
1、編寫nginx配置文件
[root@nginx conf.d]# cat game.conf
server {
listen 80;
server_name game.lol.com;
location / {
root /code;
index index.html;
}
}
2、根據配置文件,創建目錄,上傳代碼
創建目錄 mkidr /code
切換到創建的目錄 cd /code/
上傳代碼 # 使用rz -E 或者手動拖拽
解壓 unzip html5.zip
3、語法檢查並重載服務
nginx -t # nginx語法檢查
systemctl reload nginx # 平滑重啟
4、配置域名解析
配置windows上的hosts文件
10.0.0.61 game.lol.com
重載nginx服務
systemctl restart nginx # 立即重啟
systemctl reload nginx # 平滑重啟
5、nginx虛擬主機
nginx配置虛擬主機有如下三方式:
方式一:基於主機多IP方式 (用的很少,不介紹)
方式二:基於端口的配置方式 # 適合在企業內部使用
方式三:基於多個hosts名稱方式(多域名方式) #用的比較多
基於端口的配置方式
1、配置多端口的虛擬主機
[root@nginx conf.d]# cat port.conf
server {
listen 81;
location / {
root /code_81;
index index.html;
}
}
server {
listen 82;
location / {
root /code_82;
index index.html;
}
}
2、根據配置文件創建所需的目錄
[root@nginx conf.d]# mkdir /code_8{1..2}
[root@nginx conf.d]# echo '81' > /code_81/index.html
[root@nginx conf.d]# echo '82' > /code_82/index.html
3、檢查語法並重啟服務
[root@nginx conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx conf.d]# systemctl restart nginx
4、測試
[root@nginx conf.d]# curl 10.0.0.60:81
81
[root@nginx conf.d]# curl 10.0.0.60:82
82
[root@nginx conf.d]#
或者在瀏覽器輸入:
10.0.0.60:81
10.0.0.60:81
基於域名的配置方式
建議一個域名對應一個.conf文件,不要講多個域名放在同一個.conf文件中,不方便后期維護
1、准備多虛擬主機配置文件
[root@nginx conf.d]# cat test1.lol.com.conf
server {
listen 80;
server_name test1.lol.com;
location / {
root /code/test1;
index index.html;
}
}
[root@nginx conf.d]# cat test2.lol.com.conf
server {
listen 80;
server_name test2.lol.com;
location / {
root /code/test2;
index index.html;
}
}
2、根據配置問價創建對應的目錄,並檢查語法和重啟nginx # 重啟前一定要檢查語法
[root@nginx conf.d]# mkdir /code/test{1..2} -p
[root@nginx conf.d]# echo "test1_server" > /code/test1/index.html
[root@nginx conf.d]# echo "test2_server" > /code/test2/index.html
[root@nginx conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx conf.d]# systemctl restart nginx
3、在windows的hosts文件中配置域名解析
10.0.0.60 test1.lol.com
10.0.0.60 test2.lol.com
4、測試,通過瀏覽器訪問域名
test1.lol.com # 頁面顯示結果為:test1_server
test2.lol.com # 頁面顯示結果為:test2_server
PS:
當我們配置了錯誤的hosts解析,如:將 test2.lol.com,誤寫成 test3.lol.com,那么當我們在瀏覽器輸入test3.lol.com 后,瀏覽器先將域名解析成IP,然后根據IP找到nginx,但在nginx中匹配不到該域名,那么nginx就會將放在最靠前的域名配置虛擬主機返回給瀏覽器!
6、nginx日志
nginx的日志分為:
訪問日志 access.log:有優先級,server中配置了就放在server配置的路徑中,否則以http層配置的為准
錯誤日志 error.log : 路徑 /var/log/nginx/error.log
nginx日志切割配置文件:
vim /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
