1. 程式人生 > 其它 >Nginx部署,平滑新增Nginx模組,Nginx搭建小遊戲

Nginx部署,平滑新增Nginx模組,Nginx搭建小遊戲

一、Nginx概述

  1、Nginx是一個開源且高效能、可靠的http web服務、代理服務

    開源:直接獲取原始碼

    高效能:支援海量開發

    可靠:服務穩定

  2、Nginx使用的是Epoll網路模型

    select:當用戶發起一次請求,select模型就會進行一次遍歷掃描,從而導致效能低下

    Epoll:當用戶發起一次請求,epoll模型會直接進行處理,效率高,並無連線限制

  官網地址:https://nginx.org/

  軟體下載地址:https://nginx.org/download/

二、部署Nginx

  1、yum安裝

    進入官網地址--->documentation

    [root@web01 ~]# vim /etc/yum.repos.d/nginx.repo

    [root@web01 ~]# yum install nginx -y

    [root@web01 ~]# systemctl stop httpd

    [root@web01 ~]# systemctl start nginx

[root@web01 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: 
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/

  2、二進位制安裝

  3、編譯安裝

    進入官網地址--->download--->複製連結地址

    [root@web02 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz

    [root@web02 ~]# tar -xf nginx-1.20.2.tar.gz

    [root@web02 ~]# ./configure

    [root@web02 ~]# make

    [root@web02 ~]# make install

[root@web02 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments:

三、平滑增加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 --help

  [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 ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --with-http_ssl_module

四、Nginx的命令

  檢視nginx的命令:nginx -h

  -v:列印版本號

  -V:列印版本號和配置項

  -t:檢查配置檔案

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

  -T:測試配置檔案並啟動

  -q:列印錯誤日誌

  -s:操作程序

    stop:停止

    quit:退出

    reopen:重啟

    reload:過載

  -p:指定nginx的工作目錄

  -e:指定錯誤日誌路徑

  -c:指定配置檔案的路徑

  -g:設定一個全域性的nginx配置項

    [root@web01 ~]# nginx -g 'daemon off;'

五、Nginx配置檔案

  全域性配置和模組配置

[root@web01 ~]# cat /etc/nginx/nginx.conf

user  nginx;    # 指定nginx的啟動使用者
worker_processes  auto;    # 定義nginx的worker程序數;auto:CPU數量

error_log  /var/log/nginx/error.log notice;    # 錯誤日誌路徑
pid        /var/run/nginx.pid;    # pid的存放檔案路徑


events {    # 事件模組配置
    worker_connections  1024;    # 每一個worker程序最多同時接入多少個請求; use:指定nginx的網路模型
}


http {    # web服務的模組
    include       /etc/nginx/mime.types;    # 載入外部的配置項
    default_type  application/octet-stream;    # 如果找不到檔案的型別,則按照指定預設型別處理

# 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 ;

    sendfile        on;    # 高效讀取檔案
    #tcp_nopush     on;

    keepalive_timeout  65;    # 長連線保持的時間

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
[root@web01 ~]# cat /etc/nginx/conf.d/default.conf
server {    # 網址模組
    listen       80;    # 監聽的埠
    server_name  localhost;    # 定義域名

    #access_log  /var/log/nginx/host.access.log  main;

    location / {    # 訪問路徑
        root   /usr/share/nginx/html;    # 指定網址路徑
        index  index.html index.htm;    # 指定網址的索引檔案
    }

檢視日誌,json格式

[root@web01 ~]# tail -f /var/log/nginx/access.log

{"@timestamp":"2021-12-31T17:24:21+08:00","host":"192.168.15.7","service":"nginxTest","trace":"-","log":"log","clientip":"192.168.15.1","remote_user":"-","request":"GET / HTTP/1.1","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.15.7","url":"/index.html","domain":"192.168.15.7","xff":"-","referer":"-","status":"304"}

六、Nginx搭建小遊戲

6.1、超級瑪麗

  1、建立目錄

    [root@web01 ~]# cd /opt/

    [root@web01 opt]# mkdir Super_Marie

  2、上傳程式碼

[root@web01 Super_Marie]# ll
total 176
drwxr-xr-x 2 root root   329 Dec 31 17:38 images
-rw-r--r-- 1 root root  1703 Dec 31 17:38 index.html
-rw-r--r-- 1 root root 72326 Dec 31 17:38 jquery.js
-rw-r--r-- 1 root root 78982 Dec 31 17:38 QAuIByrkL.js
-rw-r--r-- 1 root root  4777 Dec 31 17:38 VNkyVaVxUV.css
-rw-r--r-- 1 root root  9539 Dec 31 17:38 wNGu2CtEMx.js

  3、編輯配置檔案

[root@web01 Super_Marie]# pwd
/opt/Super_Marie
[root@web01 Super_Marie]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# ll
total 4
-rw-r--r-- 1 root root 1072 Nov 16 23:02 default.conf
[root@web01 conf.d]# vim sup_game.conf
[root@web01 conf.d]# cat sup_game.conf
server {
    listen 80;
    server_name sup_game.test.com;
    location / {
        root /opt/Super_Marie;
    index index.html;
    }
}

  4、測試配置檔案是否正常

[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

  5、重啟Nginx

    [root@web01 conf.d]# systemctl restart nginx

  6、域名解析

    C:\Windows\System32\drivers\etc\hosts

    172.16.1.7 sup_game.test.com

6.2、象棋

  1、建立目錄

    [root@web01 opt]# mkdir Chinese_chess

  2、上傳程式碼

[root@web01 Chinese_chess]# ll
total 4
drwxr-xr-x 2 root root   22 Dec 31 18:02 css
drwxr-xr-x 4 root root   36 Dec 31 18:02 img
-rw-r--r-- 1 root root 3266 Dec 31 18:02 index.html
drwxr-xr-x 2 root root  120 Dec 31 18:02 js

  3、編輯配置檔案

[root@web01 Chinese_chess]# vim /etc/nginx/conf.d/chess_game.conf
[root@web01 Chinese_chess]# cat /etc/nginx/conf.d/chess_game.conf
server {
    listen 80;
    server_name chess_game.test.com;
    location / {
    root /opt/Chinese_chess;
    index index.html;
    }
}

  4、測試配置檔案是否正常

[root@web01 Chinese_chess]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

  5、重啟nginx

    [root@web01 Chinese_chess]# systemctl restart nginx

  6、域名解析

    C:\Windows\System32\drivers\etc\hosts

    172.16.1.7 sup_game.test.com chess_game.test.com