Nginx基本配置

2022-04-16 01:06:48 字數 3540 閱讀 6781

## 第一種:(適合原始碼編譯安裝)

nginx 啟動

nginx -s stop 停止

nginx -s reload | restart

## 第二種:(適合yum安裝)

systemctl start nginx 啟動

systemctl stop nginx 停止

systemctl restart nginx 重啟

http server location 擴充套件了解項

http{} 層下允許有多個server{}層,乙個server{}層下又允許有多個location

http{} 標籤主要用來解決使用者的請求與響應

server{} 標籤主要用來響應具體的某乙個**

location{} 標籤主要用於匹配**具體的url路徑

1、編寫nginx配置檔案

[root@nginx conf.d]# cat game.conf

server

}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 # 平滑重啟

nginx配置虛擬主機有如下三方式:

方式一:基於主機多ip方式	(用的很少,不介紹)

方式二:基於埠的配置方式 # 適合在企業內部使用

方式三:基於多個hosts名稱方式(多網域名稱方式) #用的比較多

基於埠的配置方式

1、配置多埠的虛擬主機

[root@nginx conf.d]# cat port.conf

server

}server

}2、根據配置檔案建立所需的目錄

[root@nginx conf.d]# mkdir /code_8

[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

}[root@nginx conf.d]# cat test2.lol.com.conf

server

}2、根據配置問價建立對應的目錄,並檢查語法和重啟nginx # 重啟前一定要檢查語法

[root@nginx conf.d]# mkdir /code/test -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就會將放在最靠前的網域名稱配置虛擬主機返回給瀏覽器!

nginx的日誌分為:

​	訪問日誌 access.log:有優先順序,server中配置了就放在server配置的路徑中,否則以http層配置的為準

​ 錯誤日誌 error.log : 路徑 /var/log/nginx/error.log

nginx日誌切割配置檔案:

​ vim /etc/logrotate.d/nginx

/var/log/nginx/*.log

Nginx基本配置

基本的 優化過的 配置 我們將修改的唯一檔案是nginx.conf,其中包含nginx不同模組的所有設定。你應該能夠在伺服器的 etc nginx目錄中找到nginx.conf。首先,我們將談論一些全域性設定,然後按檔案中的模組挨個來,談一下哪些設定能夠讓你在大量客戶端訪問時擁有良好的效能,為什麼它...

nginx基本配置

linux 配置 nginx 基本 1.首先安裝編輯器 因為nginx 是c 語言編寫的 yum y install make zlib zlib devel gcc c libtool openssl openssl devel 2 安裝pcre pcre 重要是讓linux 支援 rw 功能 解...

nginx 基本配置

user nobody worker processes 1 子程序,nginx啟動會有1個master程序和若干個子程序 error log logs error.log debug info notice warn error crit 日誌級別 這樣,當使用者請求 位址時,nginx 就會自動...