【nginx】Nginx的編譯安裝和配置介紹

1.首先安裝一些依賴包
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
gcc:編譯器
openssl:用於網站加密通訊
oppenssl-devel:開發軟件包
pcre:用於支持解析正則表達式
zlib:用於對數據進行解壓縮。網站之間通信時,數據先壓縮再傳輸,節省網絡帶寬

2.下載nginx安裝包
http://nginx.org/en/download.html  //下載穩定版nginx-1.14.2.tar.gz

3.解壓到如下目錄
cd /htdocs/software
tar zxf nginx-1.14.2.tar.gz

4.設置編譯方案編譯(目錄位置:cd /htdocs/software/nginx-1.14.2)
./configure --prefix=/data/nginx --user=www --group=www --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module 
--with-http_image_filter_module --with-http_gzip_static_module --with-pcre
其中:
--prefix=/data/nginx  //安裝目錄
--user=www    //設置nginx工作進程的用戶
--with-http_ssl_module   //附加了 http_ssl 模塊用於支持 Https

5.編譯和安裝
make && make install

6.校驗Nginx配置文件是否正確
/data/nginx/sbin/nginx -t  //編譯安裝時的目錄--prefix=/data/nginx
./sbin/nginx -V   // 可以看到編譯選項

7.啓動重啓關閉nginx
/data/nginx/sbin/nginx   //啓動
/data/nginx/sbin/nginx -s reload   //重啓
/data/nginx/sbin/nginx -s reload   //關閉

8.Nginx配置(nginx.conf)
sever部分:用於指定虛擬主機域名、IP和端口
location部分:用於匹配網頁位置,location繼承server,一般寫在sever裏
這邊有兩種寫法,一個是location直接寫在sever裏,另一種是location新起一個文件,然後在sever用include包含這個location文件,如
include /data/nginx/conf/location_params

sever和location具體配置方法 ,如下

nginx.conf中的sever部分:
server
{
        listen 7300;
        server_name xxx.xx.xx.xxx;
        root /data/www/s2b/supplier/dist_supplier;
        index index.html;
        include /data/nginx/conf/location_params;
}
root /data/www/s2b/supplier/dist_supplier:站點根目錄,你網站文件存放的地方
index: 請求指向index.html頁面,這個index.html需要在dist_supplier目錄下
nginx.conf同目錄下存在location_params文件
location /
{
        add_header Content-Security-Policy upgrade-insecure-requests;
        try_files $uri $uri/ @rewrites;
}
location @rewrites {
        rewrite ^(.+)$ /index.html last;
}
簡介:
1.add_header:用於設置response header
2.Content Security Policy:內容安全策略,簡稱CSP。是由W3C小組定義的一項規範,其主要作用是提供一個額外的安全層,用於檢測並削弱某些特定類型的攻擊,包括跨站腳本 (XSS) 和數據注入攻擊等
3.upgrade-insecure-requests:使http請求自動過渡成https請求,使其不會報錯
4.try_files $uri $uri/ @rewrites :
 4.1.$uri :這個是nginx的一個變量,存放着用戶訪問的地址
           比如:http://www.xxx.com/index.html, 那麼$uri就是 /index.html
 4.2.$uri/ :代表訪問的是一個目錄
           比如:http://www.xxx.com/hello/test/,那麼$uri/就是 /hello/test/
 4.3.try_files: 用戶訪問一個網址,nginx去嘗試到網站目錄讀取用戶訪問的文件,如果第一個變量存在,就直接返回;不存在繼續讀取第二個變量,如果存在,直接返回;不存在直接跳轉到第三個參數上
 參考文獻:https://www.cnblogs.com/bigberg/p/7644192.html

參考文獻:
https://www.cnblogs.com/bluestorm/p/4574688.html
https://www.cnblogs.com/wcwnina/p/8728391.html
https://www.cnblogs.com/jiangxiaobo/p/9856319.html !

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章