• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
2022
博客园    首页    新随笔    联系   管理    订阅  订阅

nginx搭建与部署 -02

nginx服务添加模块
1.安装依赖
[root@web02 nginx-1.16.1]# yum install -y openssl openssl-devel

2.再生成一次
[root@web02 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1-new --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

3.安装
[root@web02 nginx-1.16.1]# make && make install

4.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.16.1-new /usr/local/nginx

5.重启服务
[root@web02 nginx-1.16.1]# systemctl restart nginx


###.nginx升级

1.下载新版本的包
[root@web02 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz

2.解压
[root@web02 ~]# tar xf nginx-1.18.0.tar.gz

3.生成
[root@web02 nginx-1.18.0]# cd nginx-1.18.0
[root@web02 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module

4.编译安装
[root@web02 nginx-1.18.0]# make && make install

5.重做软连接
[root@web02 ~]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.18.0 /usr/local/nginx

6.重启服务
[root@web02 ~]# systemctl restart nginx
1

7.脚本编译安装合成
#/bin/bash
mkdir /server/nginx -p
wget -O/server/nginx/nginx.tar.gz http://nginx.org/download/nginx-1.18.0.tar.gz &>/dev/null
cd /server/nginx && tar xf nginx.tar.gz -C . && rm -rf nginx.tar.gz
groupadd www -g 666 && useradd www -u 666 -g 666 -M -s /sbin/nologin
cd /server/nginx/nginx-1.18.0
./configure --user=www --group=www --prefix=/usr/local/nginx-1.18.0 --without-http_rewrite_module --without-http_gzip_module
make && make install &>/dev/null
echo 'export PATH=$PATH:/usr/local/nginx-1.18.0/sbin' > /etc/profile.d/nginx.sh && source /etc/profile
source /etc/profile
#需要手动创建配置文件,添加以下内容,可用systemctl管理,启动、开机自启等
#vim /usr/lib/systemd/system/nginx.service
#[Unit]
#Description=nginx
#After=network.target
#[Service]
#Type=forking
#PIDFile=/usr/local/nginx-1.18.0/logs/nginx.pid
#ExecStart=/usr/local/nginx-1.18.0/sbin/nginx
#ExecReload=/usr/local/nginx-1.18.0/sbin/nginx -s reload
#ExecStop=/usr/local/nginx-1.18.0/sbin/nginx -s stop
#PrivateTmp=true
#[Install]
#WantedBy=multi-user.targe

systemctl restart nginx
Nginx相关文件
为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能,如下表格整理了Nginx比较重要的配置文件
Nginx主配置文件
       路径	                      类型	        作用

/etc/nginx/nginx.conf	        配置文件	nginx主配置文件
/etc/nginx/conf.d/default.conf	配置文件	默认网站配置文件

## 2.Nginx代理相关参数文件
路径	类型	作用
/etc/nginx/fastcgi_params	配置文件	Fastcgi代理配置文件
/etc/nginx/scgi_params	配置文件	scgi代理配置文件
/etc/nginx/uwsgi_params	配置文件	uwsgi代理配置文件


### 3.Nginx编码相关配置文件
路径	类型	作用
/etc/nginx/win-utf	配置文件	Nginx编码转换映射文件
/etc/nginx/koi-utf	配置文件	Nginx编码转换映射文件
/etc/nginx/koi-win	配置文件	Nginx编码转换映射文件
# /etc/nginx/mime.types	配置文件	Content-Type与扩展名

### 4.Nginx管理相关命令
路径	类型	作用
/usr/sbin/nginx	命令	Nginx命令行管理终端工具
/usr/sbin/nginx-debug	命令	Nginx命令行与终端调试工具

### 5.Nginx日志相关目录与文件
路径	类型	作用
/var/log/nginx	目录	Nginx默认存放日志目录
/etc/logrotate.d/nginx	配置文件	Nginx默认的日志切割

nginx 主配置文件

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

Nginx主配置文件整体分为三块:

CoreModule(核心模块)
EventModule(事件驱动模块)
HttpCoreModule(http内核模块)

#### nginx -t  检查配置

配置文件内容

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

 #########################核心模块####################
#指定启动的用户
user  www;
#nginx的worker进程的数量

worker_processes  1;

#指定错误日志存放的路径以及记录的级别    (调试)debug/info/notice/warn/error/emerg

error_log  /var/log/nginx/error.log warn;

#指定pid文件

pid        /var/run/nginx.pid;

########################事件驱动模块#################

events {
	#每个worker工作进程的最大连接数
    worker_connections  1024;
}


######################http内核模块(网站模块)###################

http {
	#包含,nginx可识别的文件类型
    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;
	#开启压缩
    #gzip  on;
	#包含网站的配置文件
    include /etc/nginx/conf.d/*.conf;
    
  
  [root@web01 ~]# vim /etc/nginx/conf.d/default.conf   --- 下面是这个文件里面的内容

  
  #一个server表示一个网站
    server {
    	#监听端口
        listen       80;
        #网站提供的域名
        server_name  localhost;
        #字符集
        charset utf8;
        #匹配、控制访问的网站站点
        location / {
        	#指定站点目录
            root   /usr/share/nginx/html;
            #指定默认访问的页面
            index  index.html index.htm;
        }
    }
搭建小游戏
### 1.编写史上最简单配置    -方式1
[root@web01 ~]# cat /etc/nginx/conf.d/gamem.conf 
server {
    listen 80;
    server_name localhost;

    location / {
        root /mm/tanke;
	    index index.html;
    }

}

### 2.检查配置文件

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

### 3.创建站点目录、指定配置文件

[root@web01 ~]# mkdir -p /mm/tanke
[root@web01 ~]# cd /mm/
[root@web01 mm]# ll
total 0
drwxr-xr-x 2 root root 6 Mar 30 20:43 tanke

### 4.上传代码包
[ro[root@web01 tanke] #rz
[root@web01 game] unzip tank.zip
[root@web01 tanke]# ll
total 4
drwxr-xr-x 2 root root  140 Apr 11  2015 audio
drwxr-xr-x 2 root root    6 Mar 30 20:50 Battle_City
drwxr-xr-x 2 root root   25 Apr 11  2015 css
drwxr-xr-x 2 root root   58 Sep 24  2015 images
-rw-r--r-- 1 root root 1661 Sep 24  2015 index.html
drwxr-xr-x 2 root root  261 Apr 11  2015 js
###5、修改源文件
[root@web01 mm]# cd /etc/nginx/conf.d/   --- 这2个文件发生冲突
[root@web01 conf.d]# ll
total 8
-rw-r--r-- 1 root root 1093 Oct 29 23:21 default.conf
-rw-r--r-- 1 root root  121 Mar 30 20:42 gamem.conf
[root@web01 conf.d]# mkdir a
[root@web01 conf.d]# mv default.conf a/
[root@web01 conf.d]# ll
total 4
drwxr-xr-x 2 root root  26 Mar 30 20:52 a
-rw-r--r-- 1 root root 121 Mar 30 20:42 gamem.conf
### 6、重新启动生效
[root@web01 game] systemctl restart nginx

## 7.访问页面玩游戏成功

## 方式二


# 1.修改文件
[root@web01 ~]# cat /etc/nginx/conf.d/gamem.conf 
server {
    listen 80;
    #server_name localhost;
    server_name www.games.com;   => 把lo改成calhost改成www.games.com

    location / {
        root /mm/tanke;
	index index.html;
    }

}

# 2.重启nginx

 systemctl restart nginx

# 3.配置windows下的hosts


win+r 输入drivers打开etc,hosts文件

10.0.0.7 www.game.com 

在这里插入图片描述

再搭建一个游戏
## 1.编辑配置文件

#可在一个.conf文件里添加多个游戏,每个游戏分隔单独server块,或在/game下每个游戏单独建立一个.conf文件,这样的话容易分辨:

## 1.单个文件配置多个游戏

[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# 
[root@web01 conf.d]# ll
total 4
drwxr-xr-x 2 root root  26 Mar 30 20:52 a
-rw-r--r-- 1 root root 153 Mar 30 21:06 gamem.conf
[root@web01 conf.d]# cp gamem.conf xgame.conf
[root@web01 conf.d]# ll
total 8
drwxr-xr-x 2 root root  26 Mar 30 20:52 a
-rw-r--r-- 1 root root 153 Mar 30 21:06 gamem.conf
-rw-r--r-- 1 root root 153 Mar 30 21:18 xgame.conf
### 2.编辑conf结尾的文件
# 1.[root@web01 conf.d]#vim xgame.conf
server {
    listen 80;
    server_name www.mm.com;

    location / {
        root /mm/mofang;
        index index.html;
    }
}

# 2.[root@web01 mm]# cat  /etc/nginx/conf.d/xgame.conf 
server {
    listen 80;
    #server_name localhost;
    server_name www.mm.com;

    location / {
        root /mm/mofang;
	index index.html;
    }

}
#3.上传代码包并解压
[ro[root@web01 tanke] #rz
[root@web01 game] unzip mofang.zip
[root@web01 mm]# ll
total 0
drwxr-xr-x 4 root root 45 Apr  1  2019 mofang
drwxr-xr-x 7 root root 91 Mar 30 20:50 tanke
# 4.重启nginx

 systemctl restart nginx

# 5.配置windows下的hosts    --多保存几次


win+r 输入drivers打开etc,hosts文件

10.0.0.7 www.game.comwww.mm.com

在这里插入图片描述

posted @ 2021-03-31 09:36  博客之记  阅读(70)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3