nginx部署

1.配置nginx的yum源

[root@web-8 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web-8 ~]# cat /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=truei

2.安装nginx

[root@web-8 ~]# yum install nginx -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                  | 3.6 kB  00:00:00     
epel                                                                                                                                  | 4.3 kB  00:00:00     
extras                                                                                                                                | 2.9 kB  00:00:00     
nginx-stable                                                                                                                          | 2.9 kB  00:00:00     
updates                                                                                                                               | 2.9 kB  00:00:00     
nginx-stable/7/x86_64/primary_db                                                                                                      |  97 kB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.26.1-2.el7.ngx will be installed
--> Processing Dependency: libpcre2-8.so.0()(64bit) for package: 1:nginx-1.26.1-2.el7.ngx.x86_64
--> Running transaction check
---> Package pcre2.x86_64 0:10.23-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
 Package                         Arch                             Version                                       Repository                              Size
=============================================================================================================================================================
Installing:
 nginx                           x86_64                           1:1.26.1-2.el7.ngx                            nginx-stable                           807 k
Installing for dependencies:
 pcre2                           x86_64                           10.23-2.el7                                   base                                   201 k

Transaction Summary
=============================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 1.0 M
Installed size: 3.4 M
Downloading packages:
(1/2): pcre2-10.23-2.el7.x86_64.rpm                                                                                                   | 201 kB  00:00:01     
warning: /var/cache/yum/x86_64/7/nginx-stable/packages/nginx-1.26.1-2.el7.ngx.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY00:00:01 ETA 
Public key for nginx-1.26.1-2.el7.ngx.x86_64.rpm is not installed
(2/2): nginx-1.26.1-2.el7.ngx.x86_64.rpm                                                                                              | 807 kB  00:00:02     
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                        454 kB/s | 1.0 MB  00:00:02     
Retrieving key from https://nginx.org/keys/nginx_signing.key
Importing GPG key 0xB49F6B46:
 Userid     : "nginx signing key <signing-key-2@nginx.com>"
 Fingerprint: 8540 a6f1 8833 a80e 9c16 53a4 2fd2 1310 b49f 6b46
 From       : https://nginx.org/keys/nginx_signing.key
Importing GPG key 0x7BD9BF62:
 Userid     : "nginx signing key <signing-key@nginx.com>"
 Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
 From       : https://nginx.org/keys/nginx_signing.key
Importing GPG key 0x8D88A2B3:
 Userid     : "nginx signing key <signing-key-3@nginx.com>"
 Fingerprint: 9e9b e90e acbc de69 fe9b 204c bcdc d8a3 8d88 a2b3
 From       : https://nginx.org/keys/nginx_signing.key
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : pcre2-10.23-2.el7.x86_64                                                                                                                  1/2 
  Installing : 1:nginx-1.26.1-2.el7.ngx.x86_64                                                                                                           2/2 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* https://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* https://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : pcre2-10.23-2.el7.x86_64                                                                                                                  1/2 
  Verifying  : 1:nginx-1.26.1-2.el7.ngx.x86_64                                                                                                           2/2 

Installed:
  nginx.x86_64 1:1.26.1-2.el7.ngx                                                                                                                            

Dependency Installed:
  pcre2.x86_64 0:10.23-2.el7                                                                                                                                 

Complete!

3.启动nginx加入开机自启,检查进程和运行端口

[root@web-8 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web-8 ~]# systemctl start nginx
[root@web-8 ~]# ps -ef|grep nginx
root       2420      1  0 14:40 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      2421   2420  0 14:40 ?        00:00:00 nginx: worker process
root       2423   2265  0 14:40 pts/0    00:00:00 grep --color=auto nginx
[root@web-8 ~]# ss -tunlp|grep nginx
tcp    LISTEN     0      128       *:80                    *:*                   users:(("nginx",pid=2421,fd=6),("nginx",pid=2420,fd=6))

4.基于域名的虚拟主机(案例演示)

4.1创建游戏存储目录

[root@web-8 ~]# mkdir -p /app/code/cxk

4.2 获取游戏压缩包,解压到指定目录

[root@web-8 ~]# mkdir /app/code/cxk
mkdir: cannot create directory ‘/app/code/cxk’: No such file or directory
[root@web-8 ~]# mkdir -p /app/code/cxk
[root@web-8 ~]# rz -E
rz waiting to receive.
[root@web-8 ~]# unzip cxk-ball-master.zip 
Archive:  cxk-ball-master.zip
368e2d1fe1fc7adf8595b374624f0e9b2a45a468
   creating: cxk-ball-master/
  inflating: cxk-ball-master/.gitattributes  
   creating: cxk-ball-master/.github/
   creating: cxk-ball-master/.github/ISSUE_TEMPLATE/
  inflating: cxk-ball-master/.github/ISSUE_TEMPLATE/----.md  
 extracting: cxk-ball-master/CNAME   
  inflating: cxk-ball-master/LICENSE  
  inflating: cxk-ball-master/README.md  
  inflating: cxk-ball-master/README_EN.md  
  inflating: cxk-ball-master/about.md  
   creating: cxk-ball-master/css/
  inflating: cxk-ball-master/css/common.css  
  inflating: cxk-ball-master/css/style.css  
   creating: cxk-ball-master/images/
 extracting: cxk-ball-master/images/b1.png  
 extracting: cxk-ball-master/images/b2.png  
  inflating: cxk-ball-master/images/background.jpg  
 extracting: cxk-ball-master/images/ball.png  
 extracting: cxk-ball-master/images/ballshadow.png  
 extracting: cxk-ball-master/images/block001.png  
 extracting: cxk-ball-master/images/block002.png  
  inflating: cxk-ball-master/images/block01.png  
  inflating: cxk-ball-master/images/block02.png  
 extracting: cxk-ball-master/images/block_01.png  
 extracting: cxk-ball-master/images/block_02.png  
  inflating: cxk-ball-master/images/ca0182f200f6ce71f5105c481afa756d.jpg  
  inflating: cxk-ball-master/images/f0d572d66656d92bf984f866cc5efb42.png  
 extracting: cxk-ball-master/images/paddle2_1.png  
 extracting: cxk-ball-master/images/paddle2_2.png  
 extracting: cxk-ball-master/images/paddle3_1.png  
 extracting: cxk-ball-master/images/paddle3_2.png  
 extracting: cxk-ball-master/images/paddle4_1.png  
 extracting: cxk-ball-master/images/paddle4_2.png  
 extracting: cxk-ball-master/images/paddle_1.png  
 extracting: cxk-ball-master/images/paddle_2.png  
  inflating: cxk-ball-master/images/screenshot1.png  
  inflating: cxk-ball-master/images/screenshot2.png  
  inflating: cxk-ball-master/images/test.gif  
  inflating: cxk-ball-master/index.html  
   creating: cxk-ball-master/js/
  inflating: cxk-ball-master/js/common.js  
  inflating: cxk-ball-master/js/game.js  
  inflating: cxk-ball-master/js/main.js  
  inflating: cxk-ball-master/js/scene.js  
  inflating: cxk-ball-master/js/skills.js  
[root@web-8 ~]# ll
total 1020
-rw-------. 1 root root    1348 Aug  4 14:18 anaconda-ks.cfg
drwxr-xr-x  6 root root     177 Jun 10  2019 cxk-ball-master
-rw-r--r--  1 root root 1039326 Aug 19 18:05 cxk-ball-master.zip
[root@web-8 ~]# mv cxk-ball-master/* /app/code/cxk/

4.3 在/etc/nginx/conf.d/目录下编写子配置文件

[root@web-8 ~]# vim /etc/nginx/conf.d/cxk.goodball.cn.conf
[root@web-8 ~]# cat  /etc/nginx/conf.d/cxk.goodball.cn.conf
server {
  
  listen 80;
  server_name cxk.goodball.cn;
  root /app/code/cxk;

  location / {
   
  index index.html;

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

4.4 重新加载nginx的配置文件

[root@web-8 ~]# systemctl reload nginx

4.5 在window上添加域名解析,并通过浏览器进行访问

5.当nginx部署时会出现在一个端口上同时部署了多个域名,当域名不存在或者ip访问时,则是会默认选择第一个排序的文件页面进行打开,因此,我们可以加一行配置,使得通过不存在的域名或者ip访问时出现默认的界面,在监听端口后加上default_server

[root@web-8 ~]# grep default_server  /etc/nginx/conf.d/default.conf 
    listen       80 default_server;

6.基于端口的虚拟主机

(1)编写配置文件

[root@web-8 ~]# vim /etc/nginx/conf.d/bird.goodgame.cn.conf
[root@web-8 ~]# cat /etc/nginx/conf.d/bird.goodgame.cn.conf
server {
  
  listen 81;
  server_name bird.goodgame.cn;
  root /app/code/bird;

  location / {
   
  index index.html;

    }  
}

(2)在浏览器上基于端口进行访问

7.ngixn日志

在日常工作中,会基于一个nginx部署多个网站,每个网站的错误和访问日志如果不做设置,会统一写入一个文件里面,因此,要设置相应的日志规则,让不同的网站所产生的错误日志和访问日志分别保存在不同的文件里面,编辑对应的子配置文件即可

[root@web-8 ~]# vim /etc/nginx/conf.d/bird.goodgame.cn.conf
[root@web-8 ~]# cat /etc/nginx/conf.d/bird.goodgame.cn.conf
server {
  
  listen 81;
  server_name bird.goodgame.cn;
  root /app/code/bird;
  error_log /var/log/nginx/bird.goodgame.cn-error.log notice;
  access_log /var/log/nginx/bird.goodgame.cn-access.log main;
  location / {
   
  index index.html;

    }  
}
[root@web-8 ~]# vim /etc/nginx/conf.d/cxk.goodball.cn.conf
[root@web-8 ~]# cat /etc/nginx/conf.d/cxk.goodball.cn.conf
server {
  
  listen 80;
  server_name cxk.goodball.cn;
  root /app/code/cxk;
  error_log /var/log/nginx/cxk.goodball.cn-error.log notice;
  access_log /var/log/nginx/cxk.goodball.cn-access.log main;
  location / {
   
  index index.html;

    }  
}
[root@web-8 ~]# vim /etc/nginx/nginx.conf 
[root@web-8 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web-8 ~]# ll /var/log/nginx
total 24
-rw-r----- 1 nginx adm  13074 Aug 20 15:42 access.log
-rw-r--r-- 1 root  root     0 Aug 20 15:50 bird.goodgame.cn-access.log
-rw-r--r-- 1 root  root     0 Aug 20 15:50 bird.goodgame.cn-error.log
-rw-r--r-- 1 root  root     0 Aug 20 15:50 cxk.goodball.cn-access.log
-rw-r--r-- 1 root  root     0 Aug 20 15:50 cxk.goodball.cn-error.log
-rw-r----- 1 nginx adm   5757 Aug 20 15:42 error.log

8.nginx中的location规则

(1)对于指定的uri设置对应的网段方可访问

[root@web-8 ~]# vim /etc/nginx/conf.d/cxk.goodball.cn.conf
[root@web-8 ~]# cat /etc/nginx/conf.d/cxk.goodball.cn.conf
server {
  
  listen 80;
  server_name cxk.goodball.cn;
  root /app/code/cxk;
  error_log /var/log/nginx/cxk.goodball.cn-error.log notice;
  access_log /var/log/nginx/cxk.goodball.cn-access.log main;
  location / {
   
  index index.html;

    }  
  location /admin/ {
  allow 172.16.1.0/24;
  deny all;

  }
}

(2)对于指定的js等文件设置缓存时间

[root@web-8 ~]# vim /etc/nginx/conf.d/bird.goodgame.cn.conf
[root@web-8 ~]# cat /etc/nginx/conf.d/bird.goodgame.cn.conf
server {
  
  listen 81;
  server_name bird.goodgame.cn;
  root /app/code/bird;
  error_log /var/log/nginx/bird.goodgame.cn-error.log notice;
  access_log /var/log/nginx/bird.goodgame.cn-access.log main;
  location / {
   
  index index.html;

    }
  location ~* \.(html|js|css)$ {
  expires 1d;
  }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值