nginx配置詳解

nginx配置詳解

大家好,我是酷酷的韓~
在這裏插入圖片描述
下面分享一下nginx的配置文件:

一.nginx.conf文件
1.目錄

/etc/nginx/nginx.conf

2.nginx.conf文件如下

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main '$http_user_agent' '$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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

3.nginx.conf文件詳解,針對第二條

(1)user:設計nginx服務的系統使用用戶

(2)worker_processes 工作進程數(設置成和cpu個數一致)

(3)error_log /var/log/nginx/error.log warn; 錯誤日誌

(4)pid /var/run/nginx.pid; 服務啓動時候pid

(5)events: worker_connections (每個進程允許的最大連接數,默認1024)

(6)events: use 工作進程數

(7)nginx默認的log_format記錄哪些內容。

remote_addr:表示客戶端地址
remote_user:HTTP用戶端 客戶請求nginx認證的用戶名
time_local:時間
request:請求:get/Post  http的協議版本
status:返回的狀態
body_bytes_sent:從客戶端響應給客戶端 body信息
http_referer 上一級頁面的http地址
http_user_agent:頭信息內容,表示客戶端用什麼來訪問的
http_x_forwarded_for:記錄每一級請求http信息

(8)檢查nginx配置文件格式是否正確

nginx -t /etc/nginx/nginx.conf

(9)檢查nginx配置文件路徑是否正確

nginx -c /etc/nginx/nginx.conf

二.default.conf文件

在niginx中 include /etc/nginx/conf.d/*.conf; 包含了default.conf文件

1.default.conf文件如下

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;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504 404 /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;
    #}
}

2.部分配置詳解
(1)listen 監聽端口 默認80
nginx服務啓動後直接輸入ip即可
(2)啓動頁位置

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

可以修改其/usr/share/nginx/html/index.html 再去訪問啓動頁,發現內容改變(下圖加了"(ku)")
在這裏插入圖片描述
(2)錯誤頁面
error_page 跟錯誤類型 錯誤頁面
如 error_page 404 /404.html 表示找不到則跳轉404.html頁面

後續會持續更新~

別人能做到的事,自己也可以做到。-----酷酷的韓

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