nginx基本使用與配置

一 經常使用nginx命令

nginx -s reopen # 重啓Nginx
nginx -s reload    # 從新加載Nginx配置文件,而後重啓Nginx
nginx -s stop # 強制中止Nginx服務
nginx -s quit # 處理完全部請求後再中止服務
nginx -t # 檢測配置文件是否有語法錯誤,而後退出
nginx -?,-h # 打開幫助信息
nginx -v # 顯示版本信息並退出
nginx -V # 顯示版本和配置選項信息,而後退出
nginx -t # 檢測配置文件是否有語法錯誤,而後退出
nginx -T # 檢測配置文件是否有語法錯誤,轉儲並退出
nginx -q # 在檢測配置文件期間屏蔽非錯誤信息
nginx -p prefix # 設置前綴路徑(默認是:/usr/share/nginx/)
nginx -c filename # 設置配置文件(默認是:/etc/nginx/nginx.conf)
nginx -g directives # 設置配置文件外的全局指令
killall nginx # 殺死關於全部nginx進程

二 nginx配置文件

1 配置文件總覽html

...              #全局塊

events {         #events塊
   ...
}

http      #http塊
{
    ...   #http全局塊
    server        #server塊
    { 
        ...       #server全局塊
        location [PATTERN]   #location塊
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局塊
}

2 nginx原始配置文件node

user www-data;  # 定義nginx運行用戶和用戶組,這個要設置與啓動用戶一致
worker_processes auto;  # nginx進程數,建議設置爲等於CPU總核心數;auto爲自動
pid /run/nginx.pid;  # 進程pid文件
include /etc/nginx/modules-enabled/*.conf;  # 包括這個文件裏的配置

events {
    worker_connections 768;  # 單個進程最大鏈接數;根據硬件調整;理論值:65535
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;  # nginx接收日誌
    error_log /var/log/nginx/error.log;  # nginx錯誤日誌

    gzip on;

    include /etc/nginx/conf.d/*.conf;  # 我通常把項目配置寫到這個文件夾裏面
    include /etc/nginx/sites-enabled/*;  # nginx默認頁面配置
}

3 一個簡單配置nginx

"""cd 到/etc/nginx/conf.d/,建立一個*.conf文件"""
     server {
         listen       80;
         server_name  域名;  # 配置域名

         location / {
             root   /root/.../front;  # 配置網頁根目錄
             index index.html index.htm;
         }

         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }

三 nginx相關文件位置(環境:ubuntu)

安裝好的文件位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放靜態文件
/var/log/nginx:存放日誌ubuntu

相關文章
相關標籤/搜索