Nginx 小入門記錄 之 Nginx 配置文件解讀(二)

上一小節主要是記錄一些環境準備和Nginx的安裝,接下來對Nginx基本配置進行記錄。php

查看配置文件安裝記錄html

能夠經過如下Linux命令進行查看:nginx

rpm -ql nginx

 rpm 是liunx的包管理工具,-q表明詢問模式,-l表明返回內容列表,後面是查找相關的包關鍵詞。vim

如圖所示:瀏覽器

 

 

 瞭解Linux系統都知道,安裝的包,若是須要進行相關配置,配置文件通常都在/etc目錄下。bash

配置文件解讀服務器

nginx.conf文件解讀網絡

nginx.conf 文件是Nginx的主要配置文件,在服務器環境搭建的過程當中,常常根據須要進行該文件的修改。併發

如上圖所示,能夠知道nginx.conf的文件位置,因此進入對應目錄,並用vim將文件打開app

cd /etc/nginx
vim nginx.conf  

就能看的nginx.conf的文件內容

 

 

 每項配置含義以下(下面解釋是參照jspang的)

#運行用戶,默認便是nginx,能夠不進行設置
user  nginx;
#Nginx進程,通常設置爲和CPU核數同樣
worker_processes  1;   
#錯誤日誌存放目錄
error_log  /var/log/nginx/error.log warn;
#進程pid存放位置
pid        /var/run/nginx.pid;


events {
    worker_connections  1024; # 單個後臺進程的最大併發數
}


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;   #nginx訪問日誌存放位置

    sendfile        on;   #開啓高效傳輸模式
    #tcp_nopush     on;    #減小網絡報文段的數量

    keepalive_timeout  65;  #保持鏈接的時間,也叫超時時間

    #gzip  on;  #開啓gzip壓縮

    include /etc/nginx/conf.d/*.conf; #包含的子配置項位置和文件 

你們先知道個大概,後續會針對每一項配置進行配置顯示。

default.conf文件配置

default.conf 是默認的子配置文件,是根據主配置文件最後一行加載的。

同樣,如剛開始的圖知道default.conf的目錄位置,而後打開該配置文件

cd /etc/nginx/conf.d
vim default.conf 

看的以下圖

 每項配置含義以下(下面解釋是參照jspang的)

server {
    listen       80;   #配置監聽端口
    server_name  localhost;  //配置域名

    #charset koi8-r;     
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;     #服務默認啓動目錄
        index  index.html index.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;   #錯誤狀態碼的顯示頁面,配置後須要重啓
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

根據default.conf內容所知,Nignx的根目錄爲 /usr/share/nginx/html, 進去看看都有哪些內容:

cd /usr/share/nginx/html
ls

 

 

 index.html的內容:

 

 

如上圖所示,默認裏面有50x.html、index.html兩個頁面, 裏面內容都是能夠根據本身需求進行編輯、添加的;

到這裏,先啓動Nginx服務(具體命令下一次記錄詳細說明),如今運行如下命令便可:

systemctl start nginx.service #這個命令是Linux環境下啓動服務程序的標準命令

啓動服務器後,打開瀏覽器,如上端口默認配置的是80,訪問對應的物理機的IP,個人虛擬機IP是192.168.145.128,能夠看到如下界面,這個就是默認index.html的內容。

 

 

基本配置簡單進行記錄在這吧,下一次繼續記錄Nginx的啓動、中止等經常使用命令及自定義錯誤頁。

相關文章
相關標籤/搜索