1. 程式人生 > 其它 >Web基礎入門

Web基礎入門

Web基礎入門

Nginx概述

nginx是一個開源且高效能、可靠的http web服務、代理服務。
開源 :直接獲取原始碼
高效能 :支援海量併發
可靠 :服務穩定

Nginx應用場景

nginx快速安裝

# rpm安裝(yum安裝)
     - epel倉庫
     - 官方倉庫
# 原始碼安裝

使用官方源

Nginx官方網站 nginx 官網

1.新增nginx官方源

[root@web01 ~]# vim /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=true

2.安裝nginx

[root@web01 ~]# yum -y install nginx

3.啟動nginx

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

4.檢視nginx版本號

[root@web01 ~]# nginx -v
nginx version: nginx/1.22.0

5.檢視nginx的版本和原始碼安裝生成步驟的引數有哪些

[root@web01 ~]# nginx -V
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --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'

nginx的啟停

1.啟動

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# nginx
[root@web01 ~]#/usr/sbin/nginx

2.停止

[root@web01 ~]# systemctl stop nginx
[root@web01 ~]# nginx -s stop
[root@web01 ~]#/usr/sbin/nginx -s stop

3.重新載入

[root@web01 ~]#systemctl reload nginx
[root@web01 ~]#nginx -s reload
[root@web01 ~]#/usr/sbin/nginx -s reload

nginx啟動指令碼sysctemd管理

[root@web01 ~]# vim /usr/lib/systemd/system/nginx.service 
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target

原始碼安裝,使用fpm打包,指令碼內容

[root@web01 ~]# vim post_install_nginx.sh
ln -s /application/nginx-1.20.2 /opt/nginx
echo 'PATH="/usr/local/nginx/sbin:$PATH"' > /etc/profile.d/nginx.sh
cat >> /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/application/nginx/nginx.pid
ExecStart=/application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /application/nginx/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /application/nginx/nginx.pid)"

[Install]
WantedBy=multi-user.target
EOF

nginx配置檔案

1.nginx主配置檔案

路徑 型別 作用
/etc/nginx/nginx.conf 配置檔案 nginx主配置檔案
/etc/nginx/conf.d/default.conf 配置檔案 nginx網站示例配置檔案

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 目錄 Mginx預設存放日誌目錄
/etc/logrotate.d/nginx 配置檔案 Nginx預設的日誌切割

Nginx配置檔案詳解

nginx主配置檔案

[root@web01 ~]# vim /etc/nginx/nginx.conf 
# 注意:nginx配置檔案每一行都以';'結尾
# 核心層(核心模組)、全域性配置
# nginx啟動使用者配置
user  nginx;
# nginx工作執行緒數量(cpu親和)
worker_processes  auto;
# 錯誤日誌   日誌路徑                日誌級別
error_log  /var/log/nginx/error.log notice;
# 程式啟動程序號(pid號)存放路徑
pid        /var/run/nginx.pid;

# 事件層(事件模組)
events {
	# 一個worker程序最大連線數
    worker_connections  1024;
}

# http層,http模組,網站配置
http {
	# 瀏覽器中,預設可以解析的格式(不需要下載的格式)
    include       /etc/nginx/mime.types;
    # 瀏覽器中,以下格式,點選後直接下載不解析(安裝軟體的格式)
    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;                                
                                                           # 長連線超時時間(65s)  
    keepalive_timeout  65;                                   
    # 傳輸過程中使用gzip壓縮
    #gzip  on;                                               
                                                           # 包含 nginx其他子配置檔案(網站虛擬主機配置檔案server)
    include /etc/nginx/conf.d/*.conf;                        
}                                                      

日誌格式

$remote_addr  :遠端的IP (上一個節點的IP)
- $remote_user :等路的使用者
[$time_local]  :時間
"$request" :  請求方式、請求uri、HTTp協議版本號
$status  : 狀態碼
$body_bytes_sent :流量
"$http_referer"  :跳轉地址
"$http_user_agent" :客戶端資訊
"$http_x_forwarded_for"  :記錄透傳IP地址 (獲取使用者的真實IP)

虛擬主機配置檔案

server {
	# 該網站的監聽埠
	 listen       80;
	# 該網站的IP或域名
	 server_name  localhost;
     #  該網站的日誌路徑及日誌格式
     #access_log  /var/log/nginx/host.access.log  main;
     # uri跳轉
     location / {
     	# 站點目錄
         root   /usr/share/nginx/html;
        # 預設首頁,索引頁面
         index  index.html index.htm;
     }       
 }
 
 
 # server層是網站配置層,包含在http層中
http{
	server{
		location /{
			location /xxx{}
		}
		location /api{}
		location /zls{}
	}
	server{
	}
	...
}

多虛擬主機(多web網站配置)

在企業中,是不肯用一個nginx對應一套業務,多個網站都在一個nginx中配置

基於IP的多虛擬主機

# 新增虛擬網絡卡
[root@web01 ~]# ifconfig  eth0:0 10.0.0.10
[root@web01 ~]# vim /etc/nginx/conf.d/zh.conf 
server{
        listen 80;
        server_name 10.0.0.10;

        location /{
        root /game/h5_games;
        index index.html;
}
}

[root@web01 ~]# vim /etc/nginx/conf.d/zh.conf
server{
        listen 80;
        server_name 10.0.0.7;

        location /{
        root /game/zh2;
        index index.html;
}
}



基於多埠的虛擬機器

[root@web01 ~]# vim /etc/nginx/conf.d/zh.conf 
server{
        listen 8080;
        server_name 10.0.0.7;

        location /{
                root /game/h5_games;
                index index.html;
        }
}


[root@web01 ~]# vim /etc/nginx/conf.d/zh2.conf 
server{
        listen 8081;
        server_name 10.0.0.7;

        location /{
                root /game/zh2;
                index index.html;
        }
}

基於多域名的虛擬機器

[root@web01 ~]# vim /etc/nginx/conf.d/zh.conf 
server{
        listen 80;
        server_name www.zh.com;

        location /{
                root /game/h5_games;
                index index.html;
        }
}

1.win+r開啟執行 輸入 :drivers

2.進入etc目錄

3.使用notepad++開啟hosts檔案

4.新增域名解析