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

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM