今日內容:
1.nginx的基本介紹
nginx 是一個web服務器忙厌。
2.nginx基本介紹
1.高性能贡定,近w個連接請求時,他的響應要比其他的web要快触趴。
2.高擴展性:nginx功能模塊化。官方提供了很多的優(yōu)秀模塊潮剪。
3.高可靠性:9999 99999(9代表宕機時間)
4.熱部署:不停止服務的情況下進行升級腻惠。
5.互聯(lián)網公司都選擇Nginx 代理、緩存糜芳、負載均衡飒货、靜態(tài)資源處理、動靜分離峭竣、LNMP塘辅、LNMT、LNMG 架構皆撩。
3.nginx的應用場景
1 web服務
2 負載均衡
3 代理緩存
4 安全服務 Https Lua
5 動靜分離 Nginx+Tomcat
6 靜態(tài)資源服務
4.nginx的基本組件
1.使用yum安裝
首先安裝nginx的依賴環(huán)境
vim /etc/yum.repos.d nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum install nginx -y
執(zhí)行nginx -v 即可看到安裝的版本
2.使用源碼安裝
3.nginx的網址
http://nginx.org/en/linux_packages.html#RHEL-CentOS
5.nginx的目錄結構
[root@web01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx nginx日志輪轉扣墩,用于logrotate服務的日
/etc/nginx
/etc/nginx/conf.d nginx主配置文件
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf 編碼轉換映射轉化文件(很少用到)
/etc/nginx/koi-win
/etc/nginx/mime.types 設置HTTP協(xié)議的Content-Type與拓展名對應關系
/etc/nginx/modules Nginx目錄模塊
/etc/nginx/nginx.conf #主配置文件 ( 駕駛員 )
/etc/nginx/scgi_params cgi配置相關,fastcgi配置
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug 用于配置出系統(tǒng)守護進程管理器的管理方式
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx #編譯出來的二進制文件 ( 汽車 )
/usr/sbin/nginx-debug
/var/cache/nginx Nginx的緩存目錄
/var/log/nginx #訪問日志和錯誤日志
6.nginx的配置文件
[root@web01 conf.d]# cat /etc/nginx/nginx.conf
user nginx; # nginx進程是哪個用戶來運行
worker_processes 1; # 啟動多少個worker進程
error_log /var/log/nginx/error.log warn;# 錯誤日志存在哪里( 黑匣子 )
pid /var/run/nginx.pid;# 每個進程運行起來都會存在一個pid文件扛吞,里面存放的是該進程的 ID
events {
worker_connections 1024; #worker接收的連接數(shù) 1024 * worker_process
}
http {
include /etc/nginx/mime.types;#當nginx無法識別你這個文件時呻惕,則默認一訪問就直接下載了。
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;#長連接 ( 默認長連接保持 65s )
#gzip on;#壓縮
include /etc/nginx/conf.d/*.conf;#包含/etc/nginx/conf.d/目錄下的所有以 .conf 結尾的文件滥比。
}
################################################################
server { #server表示要配置網站亚脆,每個Server{} 代表一個網站,簡稱虛擬主機
listen 80; #這個網站監(jiān)聽80端口
server_name oldxu.com; #這個網站的域名是什么
location / { #控制網站訪問的路徑
root /usr/share/nginx/html; #定義網站源代碼存放的路徑
index index.html index.htm; #默認返回哪個文件作為默認返回頁
}
}
7.nginx中的http盲泛、server濒持、location之間的關系是?
http:主要用來解決用戶的請求和響應寺滚。 比如www用戶ttt用戶
server:主要用來響應具體的某一個網站柑营。
location:用于匹配網站的uri路徑。
http{} 下面可以有多個 Server{} 每個server{} 又可以有多個 location {}
8.nginx運行一個游戲網站
1.準備一個游戲的源碼
2.準備一個目錄村视,存放我們的游戲
[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code
[root@web01 ~]# rz 游戲
[root@web01 ~]# unzip 游戲安裝包
[root@web01 ~]# cd /code/
[root@web01 code]# ls
gedou gedou.zip images index.html js README.md sound
################################################################
[root@web01 xiaolai]# ls
ceshi game h5game.zip img index.html readme.txt
3.配置Nginx
#3.1 配置監(jiān)聽的端口
#3.2 配置網站的域名
#3.3 配置用戶訪問網站路徑規(guī)則 location
#3.4 配置默認返回的頁面
[root@web01 ~]# cat /etc/nginx/conf.d/game.oldxu.com.conf
server{
listen 80;
server_name game.oldxu.com;
location / {
root /code;
index index.html;
}
}
###############################################################
[root@web01 ~]# cat /etc/nginx/conf.d/h5xiaolai.com.conf
server{
listen 80;
server_name h5xiaolai.com;
location / {
root /xiaolai;
index index.html;
}
}
4.檢查nginx的語法 {} 成對出現(xiàn)并且每一條指令的結尾都有官套;號
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5.重啟服務,讓配置生效
[root@web01 code]# systemctl restart nginx
9.nginx訪問網站的原理(詳細過程)
nginx.jpg
第一步:瀏覽器請求 game.oldxu.com
第二步:瀏覽會將請求的 game.oldxu.com --> http://game.oldxu.com/
第三步:瀏覽器對 域名進行解析 DNS解析 ( 我們域名是假的,所以奶赔,我們配置 hosts 劫持 )
第四步:瀏覽器通過隨機端口惋嚎,像服務端 80 端口建立TCP連接
第五步:瀏覽器發(fā)起HTTP的請求
第六步:請求被80端口對應的Nginx應用所接受,會交給http層纺阔,發(fā)現(xiàn)請求的域名是 game.oldxu.com
第七步:接下來檢查所有配文件瘸彤,看是否有配置文件 滿足用戶請求的域名。 server_name
第八步:滿足域名匹配之后笛钝,檢查用戶 請求的路徑质况, / 就會被location / 所匹配
第九步:返回結果,/code下面的index.html 給用戶
第十步:nginx應用程序像內核發(fā)送請求玻靡,獲取磁盤中的某個文件结榄,磁盤將數(shù)據拷貝至內核的緩存區(qū),然后在 拷貝到nginx應用進程的緩存區(qū)
第十一步:nginx應用進程封裝數(shù)據報文囤捻,回傳給客戶端瀏覽器臼朗。
10.訪問/code/gedou #出現(xiàn)403的錯誤如何解決
方式一:root /code/gedou;
方式二:game.oldxu.com/gedou/index.html --> /code/gedou/index.html
找不到為啥是403 而不是404 ?
本來要返回的是 /code/index.html 可你只有 /code 所以就403 蝎土,權限不對视哑,返回的默認主頁找不到了。