web服務之nginx部署
本期內容概要
- 了解web服務
- Nginx和Apache的對比
- 部署Nginx
內容詳細
1.什麼是web服務
Web服務是一種服務導向架構的技術,通過標準的Web協議提供服務,目的是保證不同平台的應用服務可以互操作
根據W3C的定義,Web服務應當是一個軟體系統,用以支援網路間不同機器的互動操作。
網路服務通常是許多應用程式介面所組成的,它們透過網路,例如國際互聯網的遠程伺服器端,執行客戶所提交服務的請求
web就是B/S架構
2.web伺服器軟體
# 網路模型
01 select
02 poll
03 epoll
1.apache
僅支援 select網路模型
2.Nginx
部署在Linux中 使用 epoll網路模型
官網://nginx.org/
軟體下載://nginx.org/download/




3.部署Nginx
1.安裝方式
01 yum安裝(從官網安裝 以 web01伺服器為例)
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
寫入以下內容:
[nginx-stable]
name=nginx stable repo
baseurl=//nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=//nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=//nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=//nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl start nginx
安裝完成過後 直接在瀏覽器輸入客戶端IP連接測試
02 二進位安裝
詳見 rpm安裝
//www.cnblogs.com/jgx0/p/15700136.html
03 編譯安裝(以 web02伺服器為例)
先從官網下載 nginx-1.20.2.tar.gz 壓縮包
[root@web02 ~]# wget //nginx.org/download/nginx-1.20.2.tar.gz
[root@web02 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo
寫入以下內容:
[nginx-stable]
name=nginx stable repo
baseurl=//nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=//nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=//nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=//nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web02 ~]# cd nginx-1.20.2
[root@web02 nginx-1.20.2]# ./configure
[root@web02 nginx-1.20.2]# make
[root@web02 nginx-1.20.2]# make install
因為沒有在環境變數 直接用會提示未安裝 所以要用絕對路徑執行nginx
[root@web02 nginx]# /usr/local/nginx/sbin/nginx -v

4.平滑增加Nginx模組
'''
增加模組必須重新編譯
'''
[root@web02 ~]# rm -rf nginx-1.20.2
[root@web02 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web02 ~]# cd nginx-1.20.2
[root@web02 nginx-1.20.2]# ./configure --with-http_ssl_module
一般會出現報錯 安裝報錯提示解決:
[root@web02 nginx-1.20.2]# yum install openssl openssl-devel -y
[root@web02 nginx-1.20.2]# ./configure --with-http_ssl_module
[root@web02 nginx-1.20.2]# make
[root@web02 nginx-1.20.2]# make install
[root@web02 nginx-1.20.2]# /usr/local/nginx/sbin/nginx -V
會顯示 --with-http_ssl_module 模組已經加入
5.Nginx的命令
1. -v : 列印版本號
[root@web01 ~]# nginx -v
nginx version: nginx/1.20.2
2. -V : 列印版本號和配置項
3. -t : 檢查配置文件
4. -T : 測試配置文件並啟動
5. -q : 列印錯誤日誌
6. -s : 操作進程
stop : 停止
quit : 退出
reopen : 重啟(關機之後在啟動)
reload : 重載(重載配置文件)
7. -p : 指定nginx的工作目錄
8. -e : 指定錯誤日誌路徑
9. -c : 指定配置文件的路徑
10. -g : 設置一個全局的Nginx配置項
[root@web01 ~]# nginx -g 'daemon off;'
6.Nginx配置文件
分為:
全局配置(全局的) 和 模組配置(大括弧裡面的)
[root@web01 ~]# cat /etc/nginx/nginx.conf
#####
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
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;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
#####
1.全局配置
01 user : 指定Nginx的啟動用戶
02 worker_processes : 定義Nginx的worker進程數
auto : CPU數量
03 error_log : 錯誤日誌路徑
04 pid : pid的存放文件路徑
05 events : 模組配置
worker_connections :每一個worker進程最多同時接入多少個請求
use : 指定Nginx的網路模型
06 http : web服務的模組
include : 載入外部的配置項
default_type : 如果找不到文件的類型,則按照指定默認類型處理
log_format : 定義日誌格式
例如改為json格式日誌:
log_format json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"service":"nginxTest",'
'"trace":"$upstream_http_ctx_transaction_id",'
'"log":"log",'
'"clientip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"request":"$request",'
'"http_user_agent":"$http_user_agent",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log /var/log/nginx/access.log json ;
07 sendfile : 高效讀取文件
08 keepalive_timeout : 長連接保持連接的
HTTP 1.0 短鏈接
HTTP 1.1 長連接
09 server : 網址模組
listen : 監聽的埠
server_name : 定義域名
location : 訪問路徑
root : 指定網址路徑
index : 指定網址的索引文件
7.超級瑪麗和象棋遊戲搭建
1.上傳程式碼
2.編輯配置文件
創建遊戲目錄:
[root@web01 opt]# mkdir /opt/Super_Marie
[root@web01 opt]# mkdir /opt/Chinese_chess
[root@web01 opt]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim game.conf
寫入以下內容:
server {
listen 80;
server_name game.Marie.com;
location / {
root /opt/Super_Marie;
index index.html;
}
}
[root@web01 conf.d]# vim game1.conf
寫入以下內容:
server {
listen 80;
server_name game.chess.com;
location / {
root /opt/Chinese_chess;
index index.html;
}
}
3.測試配置文件是否正常
[root@web01 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
4.重啟Nginx
[root@web01 conf.d]# systemctl restart nginx
5.Windows域名解析
目錄: C:\Windows\System32\drivers\etc\hosts
文本內寫入並保存(如果沒有該文件:hosts 直接創建普通文本即可):
192.168.15.7 game.Marie.com
192.168.15.7 game.chess.com
6.測試
瀏覽器輸入網址:
game.Marie.com
game.chess.com
正常打開遊戲就沒問題
