Nginx目錄結構與配置文件詳解

Nginx安裝

具體安裝nginx請移步:nginx部署

安裝依賴

安裝pcre依賴軟件
[root@ubuntu ~]# yum install -y pcre pcre-devel 		//外網情況使用此安裝方式
[root@ubuntu ~]# rpm -ivh pcre 		//內網情況使用此安裝方式
[root@ubuntu ~]# rpm -ivh pcre-devel		//內網情況使用此安裝方式
#編譯安裝pcre依賴軟件(注意編譯安裝軟件包需要開發工具的支持安裝nginx需要make、gcc、c++支持)
#下載地址: https://ftp.pcre.org/pub/pcre/		//根據相關情況進行選擇版本下載
[root@ubuntu ~]# wget https://ftp.pcre.org/pub/pcre/pcre-8.00.tar.gz		//下載pcre軟件包
[root@ubuntu ~]# tar zxf pcre-7.80.tar.gz		//解壓軟件包
[root@ubuntu ~]# cd pcre-7.80 		//進入相關目錄
[root@ubuntu ~]# ./configure		//配置
[root@ubuntu ~]# make && make install		//編譯並編譯部署 
安裝openssl依賴軟件
[root@ubuntu ~]# yum install -y openssl openssl-devel		//外網情況使用此安裝方式
[root@ubuntu ~]# rpm -ivh openssl		//內網情況使用此安裝方式
[root@ubuntu ~]# rpm -ivh openssl-devel			//內網情況使用此安裝方式
#編譯安裝與pcre幾乎一致,請參考上一小節

安裝Nginx

nginx下載地址:nginx官網地址,下載nginx時需要注意版本問題,nginx分爲三個版本:穩定版、開發版和老版本穩定版

檢查依賴是否安裝
[root@ubuntu ~]# rpm -qa pcre-devel pcre
 pcre-devel-7.8-6.el6.x86_64       #pcre的devel字符串包已經安裝
 pcre-7.8-6.el6.x86_64             #pcre軟件包已經安裝
[root@ubuntu ~]# rpm -qa openssl-devel openssl
 openssl-1.0.1e-30.el6.x86_64        #openssl的包已經安裝
 openssl-devel-1.1.13-30.el6.x86_64		#openssl的devel字符串包已經安裝
安裝nginx
[root@ubuntu:/opt]# wget -q http://nginx.org/download/nginx-1.17.6.tar.gz		//下載軟件包
[root@ubuntu:/opt]# ls  nginx-1.17.6.tar.gz    	//查看是否下載成功
-rw-r--r-- 1 root root 1037527 Nov 19 14:25 nginx-1.17.6.tar.gz
[root@ubuntu:/opt]# useradd nginx -s /sbin/nologin -M		//創建nginx執行用戶
[root@ubuntu:/opt]# tar xf nginx-1.17.6.tar.gz		//解壓nginx軟件包
[root@ubuntu:/opt]# cd nginx-1.17.6		//進入解壓目錄
[root@ubuntu nginx-1.17.6]#./configure  --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module  --with-http_ssl_module		//配置nginx軟件包

#具體配置模塊講解請移步nginx編譯配置模塊大全

[root@ubuntu nginx-1.17.6]# make			//編譯nginx軟件包
[root@ubuntu nginx-1.17.6]# make install		//部署nginx軟件包

啓動nginx檢查是否安裝成功

檢查nginx語法
root@ubuntu:~# /usr/local/nginx/sbin/nginx -t		//查看nginx配置文件是否正確
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok		//表示語法正確
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful	//test成功
啓動nginx
root@ubuntu:~# /usr/local/nginx/sbin/nginx 		//直接啓動就可以

具體的啓動方式請移步:nginx部署

查看Nginx端口是否啓動
root@ubuntu:~# lsof -i :80		//查看相關端口
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   23658   root    6u  IPv4  98258      0t0  TCP *:http (LISTEN)
nginx   23659 nobody    6u  IPv4  98258      0t0  TCP *:http (LISTEN)
root@ubuntu:~# netstat -anpl | grep nginx		//查看nginx的相關端口
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23658/nginx: master 
unix  3      [ ]         STREAM     CONNECTED     98314    23658/nginx: master  
unix  3      [ ]         STREAM     CONNECTED     98315    23658/nginx: master  
root@ubuntu:~# 
訪問nginx
root@ubuntu:~# curl 127.0.0.1		//訪問nginx
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

nginx目錄結構說明

root@ubuntu:~# tree /usr/local/nginx/
/usr/local/nginx/
├── client_body_temp
├── conf			#存放一系列配置文件的目錄
│   ├── fastcgi.conf		#fastcgi程序相關配置文件
│   ├── fastcgi.conf.default	#fastcgi程序相關配置文件備份
│   ├── fastcgi_params		#fastcgi程序參數文件
│   ├── fastcgi_params.default		#fastcgi程序參數文件備份
│   ├── koi-utf		#編碼映射文件
│   ├── koi-win		#編碼映射文件
│   ├── mime.types		#媒體類型控制文件
│   ├── mime.types.default		#媒體類型控制文件備份
│   ├── nginx.conf		#主配置文件
│   ├── nginx.conf.default		#主配置文件備份
│   ├── scgi_params		#scgi程序相關配置文件
│   ├── scgi_params.default		#scgi程序相關配置文件備份
│   ├── uwsgi_params		#uwsgi程序相關配置文件
│   ├── uwsgi_params.default		#uwsgi程序相關配置文件備份
│   └── win-utf		#編碼映射文件
├── fastcgi_temp		#存放fastcgi程序臨時文件
├── html		#存放網頁文檔
│   ├── 50x.html		#錯誤頁碼顯示網頁文件
│   └── index.html		#網頁的首頁文件
├── logs		#存放nginx的日誌文件
│   ├── access.log		#默認訪問日誌
│   ├── error.log		#錯誤日誌
│   └── nginx.pid		#nginx pid文件
├── proxy_temp		#代理相關臨時文件
├── sbin		#存放啓動程序
│   └── nginx		#nginx啓動程序
├── scgi_temp		#存放scgi程序臨時文件
└── uwsgi_temp		#存放uwsgi程序臨時文件

9 directories, 21 files

主配置文件說明

#user  nobody;		//定義執行nginx程序的用戶
worker_processes  1;		#指定nginx進程數

#錯誤日誌定義類型有:debug、info、notice、warn、error、crit
#error_log  logs/error.log;		//指定錯誤日誌目錄
#error_log  logs/error.log  notice;		//指定notice類型的錯誤日誌目錄
#error_log  logs/error.log  info;		//指定全局錯誤日誌目錄
#worker_rlimit_nofile 1024; 		//指定但進程最多打開文件數量
#pid        logs/nginx.pid;		//指定進程文件的目錄


events {			#事件區塊
#事件模型有:kqueue、rtsig、epoll、/dev/poll、select、poll
	use epoll;		#指定參考事件模型
    worker_connections  1024;		#指定單進程最大鏈接數
}


http {		#http區塊
    include       mime.types;		#指定nginx支持的媒體類型
    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  logs/access.log  main;		//指定nginx的訪問日誌目錄

    sendfile        on;		#開啓高效的傳輸模式
    #tcp_nopush     on;		//防止網絡阻塞

    #keepalive_timeout  0;		//鏈接超時事件
    keepalive_timeout  65;

    #gzip  on;		//開啓gzip壓縮輸出
	#gzip_min_length 1k;		//最小壓縮文件大小
	#gzip_buffers 4 16k;		//壓縮緩衝區
	#gzip_http_version 1.0;		//壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
 	#gzip_comp_level 2;		//壓縮等級
 	#gzip_types text/x-javascript text/css application/xml;		//壓縮類型,默認就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
	#gzip_vary on;		//增加響應頭”Vary: Accept-Encoding”
	#limit_zone crawler $binary_remote_addr 10m;		//開啓限制IP連接數的時候需要使用
	#FastCGI程序相關參數
	#配置FastCGI程序相關參數是爲了改善網站的性能:減少資源佔用,提高訪問速度。
 	#fastcgi_connect_timeout 300;		//連接到後端fastcgi超時時間
 	#fastcgi_send_timeout 300;		//向fastcgi請求超時時間(這個指定值已經完成兩次握手後向fastcgi傳送請求的超時時間)
 	#fastcgi_read_timeout 300;		//接收fastcgi應答超時時間,同理也是2次握手後
 	#fastcgi_buffer_size 64k;		//讀取fastcgi應答第一部分需要多大緩衝區,該值表示使用1個64kb的緩衝區讀取應答第一部分(應答頭),可以設置爲fastcgi_buffers選項緩衝區大小
 	#fastcgi_buffers 4 64k;		//指定本地需要多少和多大的緩衝區來緩衝fastcgi應答請求,假設一個php或java腳本所產生頁面大小爲256kb,那麼會爲其分配4個64kb的緩衝來緩存;若頁面大於256kb,那麼大於的256kb的部分會緩存到fastcgi_temp指定路徑中,這並非是個好辦法,內存數據處理快於硬盤,一般該值應該爲站點中php/java腳本所產生頁面大小中間值,如果站點大部分腳本所產生的頁面大小爲256kb,那麼可把值設置爲16 16k,4 64k等
 	#fastcgi_busy_buffers_size 128k;		//默認值是fastcgi_buffer的2倍
 	#fastcgi_temp_file_write_size 128k;		//寫入緩存文件使用多大的數據塊,默認值是fastcgi_buffer的2倍
 	#fastcgi_cache TEST;		//開啓fastcgi緩存併爲其指定爲TEST名稱,降低cpu負載,防止502錯誤發生.
#應答代碼緩存時間,200和302應答緩存爲1個小時,301一天,其他1分鐘
	#fastcgi_cache_valid 200 302 1h;
	#fastcgi_cache_valid 301 1d;
	#fastcgi_cache_valid any 1m;
#主機配置
    server {		#server區塊,每一個區塊表示一個虛擬主機
        listen       80;		#偵聽端口
        server_name  localhost;		#配置此主機的域名

        #charset koi8-r;		//配置字符編碼

        #access_log  logs/host.access.log  main;		//配置默認訪問日誌目錄

        location / {		#location區塊 通過指定模式來與客戶端請求的URI相匹配
            root   html;		#配置站點根目錄
            index  index.html index.htm;		#默認文件
        }

        #error_page  404              /404.html;	//當發生404錯誤的時候能夠顯示一個預定義的uri

        # redirect server error pages to the static page /50x.html		//將服務器錯誤頁重定向到靜態頁/50x.html
        #
        error_page   500 502 503 504  /50x.html;		#當發生其中錯誤的時候能夠顯示一個預定義的uri

#反向代理
		#location / {
			#proxy_pass http://127.0.0.1:88;	//指定代理地址
			#proxy_redirect off;		//當上遊服務器返回的響應是重定向或刷新請求(如HTTP響應碼是301或者302)時,proxy_redirect可以重設HTTP頭部的location或refresh字段。可選參數default、off、redirect replacement]
	#後端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
			#proxy_set_header X-Real-IP $remote_addr; 	
			#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			#proxy_set_header Host $host;	//允許重新定義或者添加發往後端服務器的請求頭
			#client_max_body_size 10m;      //允許客戶端請求的最大單文件字節數
			#client_body_buffer_size 128k;		//緩衝區代理緩衝用戶端請求的最大字節數
			#proxy_connect_timeout 90;		//nginx跟後端服務器連接超時時間(代理連接超時)
			#proxy_send_timeout 90;		 //後端服務器數據回傳時間(代理髮送超時)
			#proxy_read_timeout 90;		//連接成功後,後端服務器響應時間(代理接收超時)
			#proxy_buffer_size 4k; 		//設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
			#proxy_buffers 4 32k; 		//proxy_buffers緩衝區,網頁平均在32k以下的設置
			#proxy_busy_buffers_size 64k; 		//高負荷下緩衝大小(proxy_buffers*2)
			#proxy_temp_file_write_size 64k;		//設定緩存文件夾大小,大於這個值,將從upstream服務器傳
106 
		#}
		
 
#設置js和css緩存時間
		#location ~ .*\.(js|css)?$
		#{
			#expires 2h;
		# }

#設置圖片緩存事件
      	#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
       	#{
       		#expires 5d;
       	# }

#監控Nginx狀態的地址
	#location /NginxStatus {
		#stub_status on;		//開啓監控狀態
		#access_log on;			//開啓默認訪問監控
		#auth_basic "NginxStatus";		//設置驗證模塊
		#auth_basic_user_file conf/htpasswd;		//htpasswd文件的內容可以用apache提供的htpasswd工具來產生。
	#}

#動靜分離配置
#所有jsp的頁面均交由tomcat處理
	#location ~ .(jsp|jspx|do)?$ {
		#proxy_set_header Host $host;
		#proxy_set_header X-Real-IP $remote_addr;
		#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		#proxy_pass http://127.0.0.1:8080;
	# }

#所有靜態文件由nginx直接讀取不經過tomcat
	#location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$#
	#{
		#expires 10d; 
	#}
	
	#location ~ .*.(js|css)?${
		#expires 2h;
	#}
    
}
發佈了25 篇原創文章 · 獲贊 45 · 訪問量 7182
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章