nginx搭建靜態網站

Nginx (engine x) 是一個高性能的HTTP和反向代理web服務,經常使用於負載均衡構架,以提升網站的併發量,概念不過多介紹,更多細節請自行百度,html

本文是純操做案例,假設你已經知道什麼是nginx而且知道它用來幹什麼,那麼你能夠按照本文步驟來使用nginx搭建出一個靜態網站html5

以此你能夠對nginx有一個直觀的認識linux

 

一 安裝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.開始安裝

上述提供了兩個不一樣版本nginx

直接執行 yum install nginx 將安裝穩定版 stableweb

 
 
 
xxxxxxxxxx
 
 
 
 
yum install nginx -y
 
 

若是要安裝 主力版本相關的包可用將主力版的enable設置爲1vim

 
 
 
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
 
 

錯誤解決windows

若是安裝過中出現Cannot find a valid baseurl for repo: base/7/x86_64 錯誤centos

咱們須要添加新的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

相關文章
相關標籤/搜索