nginx搭建静态网站

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务,常用于负载均衡构架,以提高网站的并发量,概念不过多介绍,更多细节请自行百度,

本文是纯操作案例,假设你已经知道什么是nginx并且知道它用来干什么,那么你可以按照本文步骤来使用nginx搭建出一个静态网站

以此你可以对nginx有一个直观的认识

 

一 安装nginx

1.添加nginx仓库

 
 
 
x
 
 
 
 
1.1创建仓库文件
touch /etc/yum.repos.d/nginx.repo
1.2创建仓库信息
vim 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
# 主力版
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
 

2.开始安装

上述提供了两个不同版本

直接执行 yum install nginx 将安装稳定版 stable

 
 
 
xxxxxxxxxx
 
 
 
 
yum install nginx -y
 

如果要安装 主力版本相关的包可用将主力版的enable设置为1

 
 
 
xxxxxxxxxx
 
 
 
 
# 主力版
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
 

错误解决

如果安装过中出现Cannot find a valid baseurl for repo: base/7/x86_64 错误

我们需要添加新的DNS服务器地址

 
 
 
xxxxxxxxxx
 
 
 
 
echo "nameserver 114.114.114.114" >> /etc/resolv.conf
 

然后重新执行安装命令即可

其他系统参考

https://nginx.org/en/linux_packages.html

 

3.启动ngxin

 
 
 
xxxxxxxxxx
 
 
 
 
# 启动
nginx
# 查询进程是否启动
ps -aux|grep nginx
# 更近一步  尝试本地访问
wget 127.0.0.1:80
#2019-06-19 16:49:01 (31.8 MB/s) - 已保存 “index.html.1” [612/612])
# 显示文件以保存则表明nginx启动成功
 

4.主机访问

直接使用浏览器访问主机ip如果看到欢迎界面则启动成功

image-20190619165032611

 

开放端口

若访问失败则说明防火墙启动且没有开放相应的端口

 
 
 
xxxxxxxxxx
 
 
 
 
1.开放端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
2.使规则生效
firewall-cmd --reload
 

再次通过浏览器应该可以访问了!

 

 

 

防火墙设置

CentOS系统在初始状态下是打开了防火墙的并且不允许任何流量的出入,当然 22端口的基础端口是开放的

这就需要我们自己来开启需要的端口,nginx需要代理HTTP/HTTPS请求 所以我们需要开放相应端口

开启与关闭

 
 
 
xxxxxxxxxx
 
 
 
 
1. 停止
systemctl stop firewalld.service 
2. 启动
systemctl start firewalld.service 
3. 重启
systemctl restart firewalld.service
4. 查看状态: 
systemctl status firewalld 
5.禁止firewall开机启动
systemctl disable firewalld
6. 设置开机启用防火墙:
systemctl enable firewalld.service
 

查看状态与规则

 
 
 
xxxxxxxxxx
 
 
 
 
1. 查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state              
2. 查看防火墙规则(只显示/etc/firewalld/zones/public.xml中防火墙策略)
firewall-cmd --list-all           
3. 查看所有的防火墙策略(即显示/etc/firewalld/zones/下的所有策略)
firewall-cmd --list-all-zones     
4. 重新加载配置文件
firewall-cmd --reload             
 

添加与删除规则

 
 
 
xxxxxxxxxx
 
 
 
 
1. 添加(--permanent永久生效,没有此参数重启后失效)
firewall-cmd --zone=public --add-port=80/tcp --permanent
2. 重新载入(修改规则后使其生效)
firewall-cmd --reload
3. 查看
firewall-cmd --zone=public --query-port=80/tcp
4. 删除
firewall-cmd --zone=public --remove-port=80/tcp --permanent
 

 

二 基础命令

启动与关闭命令

 
 
 
xxxxxxxxxx
 
 
 
 
查看nginx目录结构
rpm -ql nginx
启动
nginx
停止
nginx -s stop
重启
nginx -s reload  # 平滑重启
方式二:
systemctl start nginx
systemctl stop nginx
systemctl restart nginx # 直接重启
# 平滑重启服务  会先将当前任务处理完毕在重启
systemctl reload nginx
注意:两种方式不能混合使用
强制结束
pkill nginx
 

三 配置文件解析

 
 
 
xxxxxxxxxx
 
 
 
 
#核心模块配置
user www; #nginx进程使用的用户
worker_processes 1; #nginx运行的worker进程数量(建议与CPU数量一致或auto)
err_log /log/nginx/error.log#错误日志存放目录
pid        /var/run/nginx.pid;#nginx进程的ip
#事件模块配置
events {
    worker_connections  1024; #一个worker最大的链接数量
  use epool;#使用的网络模型为epool 默认
}
# http模块配置
http {
    include       /etc/nginx/mime.types;  #文件后缀与 响应数据类型  的映射表
    default_type  application/octet-stream; #当后缀找不到映射时 使用的默认类型  stream即文件下载
# 指定日志输出格式  $表示取nginx内部变量
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
# 指定日志路径 并指定需要使用的格式为main
    access_log  /var/log/nginx/access.log  main;
    sendfile        on; # 启用高效文件传输 nginx内部提供的方法
    #tcp_nopush     on;
    keepalive_timeout  65; #会话超时时间
    #gzip  on;#是否开启压缩功能
    include /etc/nginx/conf.d/*.conf; # 包含其他位置的配置文件 (server)
}
 

server配置

 
 
 
xxxxxxxxxx
 
 
 
 
# server配置项位于http之内 之所以分开是为了 方便管理
server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;  指定编码方式
    #access_log  /var/log/nginx/host.access.log  main;  #单独指定该服务的日志路径
  # 转发路径
    location / {                       # 10.0.0.11  == http://10.0.0.11:80/   /表示跟
        root   /usr/share/nginx/html;  # 访问路径为/时 到/usr/share/nginx/html;下找文件 
       # /将被替换为 root 后的路径
        index  index.html index.htm;   # 默认的主页文件 
      # 该配置表示当访问了地址为10.0.0.11时将返回
                                       # /usr/share/nginx/html/index.html 或/ htm文件
    }
    #error_page  404              /404.html; # 遇到404时要返回的页面
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html; # 当遇到5xx服务器错误时 返回
    location = /50x.html { #/usr/share/nginx/html/50x.html
        root   /usr/share/nginx/html;     
    }
  
  # 一个server中可以包含多个location配置项
}
 

 

四 nginx 部署静态网站案例:

 
 
 
xxxxxxxxxx
 
 
 
 
1.保持主配置文件为默认内容
2.创建自己的server配置文件
vim /etc/nginx/conf.d/game.conf
# 内容:
server{
listen 80;  #监听的端口
server_name game.oldboy.com; #监听的域名 

location / {
root /game/html; #网站所在路径
index index.html; #默认的首页文件
}
}
3.根据配置创建网站目录
mkdir /game/html
4.上传文件
在客户机执行命令
scp /Volumes/yh/linux备课视频/day31-老男孩教育3期-nginx基础/html5.zip  root@10.0.0.11:/game/
输入密码
5.解压文件
unzip /game/html5.zip -d /game/html/
6.将网站目录移交给nginx 用户
用于ngin会启动worker进程来执行任务,所以必须使得woker进程拥有目录的访问和执行权限 
chown nginx.nginx -R /game/
7.重启nginx 
systemctl reload ginx
9.由于我们是局域网环境无法直接使用域名来访问,所以我们需要自己来添加域名解析映射
mac 修改方式:
sudo vim /etc/hosts
在最后追加内容:
10.0.0.11    game.oldboy.com
windows 修改方式:
文件位于:C:\Windows\System32\drivers\etc\hosts
打开在最后加入 10.0.0.11    game.oldboy.com
如果无法保存 可以在桌面创建hosts修改后覆盖到原位置

10.通过浏览器访问game.oldboy.com 一切顺利的话将看到一下内容:
 

image-20190619230017732

 

 

 

 

 

posted @ 2019-06-19 23:18  CoderJerry  阅读(9614)  评论(1编辑  收藏  举报