1.什么是nginx
Nginx是一款輕量級的Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器橙弱,在BSD-like 協(xié)議下發(fā)行具伍。其特點是占有內(nèi)存少,并發(fā)能力強(qiáng)纵散,事實上nginx的并發(fā)能力確實在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好
2.常見的web服務(wù)器
Apache梳码、Nginx隐圾、Lighttpd、Tomcat边翁、IBM WebSphere
3.nginx的應(yīng)用場景
https://blog.csdn.net/vbirdbest/article/details/80913319
? 1.代理
? 2.負(fù)載均衡
? 3.代理緩存 (proxy_cache)
? 4.靜態(tài)資源
? 5.動靜分離
? 6.Https
4.nginx 配置文件
-
配置文件:vim /etc/nginx/nginx.conf
user nginx;#進(jìn)程用戶身份 worker_processes 1;#工作的進(jìn)程數(shù) error_log /var/log/nginx/error.log warn;#錯誤日志 pid /var/run/nginx.pid;#進(jìn)程運行后產(chǎn)生的進(jìn)程號 events { #時間模型 worker_connections 1024;#每個work能支持的連接數(shù) user epoll #使用的網(wǎng)絡(luò)模型 } http { #接受用的http請求 include /etc/nginx/mime.types;靜態(tài)資源文件 default_type application/octet-stream;默認(rèn)類型 #日志相關(guān) 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;#包含的文件路徑
5.nginx服務(wù)
-
1)安裝:
安裝源: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
安裝:yum install nginx -y
-
2)配置:vim /etc/nginx/conf.d/wxb.conf
server{ listen 80; server_name wangxub.cn; location / { root /code; index index.html; } }
-
3)啟動:
systemctl start nginx systenctl enable nginx
-
PS: Nginx中的http、server硕盹、location之間的關(guān)系是?
http?? 標(biāo)簽主要用來解決用戶的請求與響應(yīng)符匾。 server?? 標(biāo)簽主要用來響應(yīng)具體的某一個網(wǎng)站。 location ? 標(biāo)簽主要用于匹配網(wǎng)站具體url路徑瘩例。
6.Nginx 搭建 游戲網(wǎng)站
-
1)注釋掉之前的默認(rèn)網(wǎng)站
[root@web01 html]# cd /etc/nginx/conf.d/ [root@web01 conf.d]# gzip default.conf
-
2)編寫游戲網(wǎng)站Nginx配置文件
[root@web01 conf.d]# cat game.oldxu.com.conf
server {
listen 80; #該網(wǎng)站提供訪問的端口
server_name game.oldxu.com; #訪問該網(wǎng)站的域名[root@web01 conf.d]# cat game.oldxu.com.conf server { listen 80; #該網(wǎng)站提供訪問的端口 server_name game.oldxu.com; #訪問該網(wǎng)站的域名 location / { root /code; index index.html; } }
-
3.根據(jù)Nginx的配置文件,初始化
[root@web01 conf.d]# mkdir /code
-
4.上傳代碼
[root@web01 conf.d]# cd /code/ [root@web01 code]# rz html5.zip [root@web01 code]# unzip html5.zip
-
5.檢測語法
[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
-
6.重載服務(wù)
[root@web01 code]# systemctl restart nginx
7.配置域名解析
7.Nginx訪問的整體流程
-
http:// game.oldxu.com / game/yibihua/index.html
請求的uri: /game/yibihua/index.html
真實映射位置: /code/game/yibihua/index.html
8.Nginx 搭建 多個游戲網(wǎng)站 ---> 虛擬主機(jī)
Nginx配置虛擬主機(jī)有如下三種方式:
方式一啊胶、基于主機(jī)多IP方式 10.0.0.7 172.16.1.7
方式二、基于端口的配置方式 80 81 82 83
方式三垛贤、基于名稱方式(多域名方式) test1 test2 test3-
方式一焰坪、基于主機(jī)多IP方式
[root@web01 conf.d]# cat ip_eth0.conf server { listen 10.0.0.7:80; location / { root /ip1; index index.html; } } server { listen 172.16.1.7:80; location / { root /ip2; index index.html; } } [root@web01 conf.d]# mkdir /ip1 /ip2 [root@web01 conf.d]# echo "10...." > /ip1/index.html [root@web01 conf.d]# echo "172...." > /ip2/index.html [root@web01 conf.d]# systemctl restart nginx [root@web01 ~]# curl http://10.0.0.7 10.... [root@web01 ~]# curl http://172.16.1.7 172....
方式二、基于端口的配置方式 81 82 83
公司內(nèi)部有多套系統(tǒng),希望部署在一臺服務(wù)器上, 而內(nèi)網(wǎng)又沒有域名.
所以,我們可以通過相同IP,不同的端口,訪問不同的網(wǎng)站頁面.
[root@web01 conf.d]# cat port.conf
server {
listen 81;
[root@web01 conf.d]# cat port.conf
server {
listen 81;
location / {
root /81;
index index.html;
}
}
server {
listen 82;
location / {
root /82;
index index.html;
}
}
server {
listen 83;
location / {
root /83;
index index.html;
}
}
[root@web01 conf.d]# mkdir /81 /82 /83
[root@web01 conf.d]# echo "81" > /81/index.html
[root@web01 conf.d]# echo "82" > /82/index.html
[root@web01 conf.d]# echo "83" > /83/index.html