nginx配置指南

1 基本配置

# 運行用戶
user www www;
# 啓動進程,通常設置成和cpu的數量相等
worker_processes 2; #設置值和CPU核心數一致
# 全局錯誤日誌
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日誌位置和日誌級別
# pid文件
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

# 工作模式及連接上限
events
{
  # epoll是多路複用IO中的一種方式,僅適用於linux2.6以上內核,可以大大提高nginx的性能
  use epoll;
  # 單個後臺worker process進程的最大併發鏈接數
  worker_connections 65535;
}

# http模塊
http
{
  #設定mime類型,類型由mime.type文件定義
  include 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  logs/access.log  main;
  
    #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,
  #對於普通應用,必須設爲 on,
  #如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,
  #以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
  sendfile        on;
  #tcp_nopush     on;

  #連接超時時間
  keepalive_timeout  65;
    #開啓gzip壓縮
  gzip  on;
 
 #設定虛擬主機配置
 server
  {
    listen 80;#監聽端口
    server_name www.litanghui;#域名
    index index.html index.htm index.php; #首頁的文件名稱
    root /www/litanghui;#服務器的默認網站根目錄位置
    #默認請求
    location / {
        #定義首頁索引文件的名稱
        index index.php index.html index.htm;   

    }
    #靜態圖像文件
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
    }
    #靜態js和css文件
    location ~ .*\.(js|css)?$
    {
      expires 15d;
    }
  }
  #開啓https配置
  server
  {

      listen 443;
      server_name xxx.com;

      #開啓ssl
      ssl on;
      #證書
      ssl_certificate /xxx/fullchain.pem;
      #key
      ssl_certificate_key /xxx/privkey.pem;
            #協議
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #套件
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    
  }
}

2 基本命令

2.1 重新載入配置文件

nginx -s reload

2.2 重啓nginx

nginx -s reopen

2.3 停止nginx

nginx -s stop

其他說明

  1. 在Mac上使用brew安裝nginx軟件的目錄地址爲:/usr/local/Cellar/nginx
  2. 在Mac上使用brew安裝nginx的配置文件的目錄地址爲:/usr/local/etc/nginx/nginx.conf
  3. 正向代理:"它代理的是客戶端,代客戶端發出請求"
    • 訪問原來無法訪問的資源,如Google
    • 可以做緩存,加速訪問資源
    • 對客戶端訪問授權,上網進行認證
    • 代理可以記錄用戶訪問記錄,對外隱藏用戶信息
  4. 正向代理:"它代理的是服務端,代服務端接收請求"
    • 保證內網的安全,通常將反向代理作爲公網訪問地址,web服務器是內網
    • 負載均衡,通過反向代理服務器來優化網站的負載

參考文章

  1. 在mac下安裝nginx
  2. nginx相關介紹
  3. 免費域名證書 + nginx開啓https訪問
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章