1. 程式人生 > 實用技巧 >linux 安裝 nginx

linux 安裝 nginx

nginx open source 下載地址 nginx.org

下載好包後解壓,然後就可以開始了

1. 是否安裝了gcc

yum list|grep gcc-c++

沒安裝安一下,下面的到第5步過程都一樣
yum -y install gcc-c++

3. 檢查 PCRE

yum list|grep pcre pcre-devel
yum -y install pcre pcre-devel

4. 檢查 zlib zlib-devel

yum list|grep zlib zlib-devel
yum install -y zlib zlib-devel

5. jiancha openssl

yum list|grep openssl openssl-devel
yum install -y openssl openssl-devel

6. ./config 設定編譯引數

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

7. 編譯

make
make install

nginx 基本操作

#啟動
[root@localhost ~]# /usr/local/nginx/sbin/nginx
#停止/重啟
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
# 建議使用完整停止 quit
#命令幫助
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
#驗證配置檔案
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
#過載配置檔案
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

conf/nginx.conf 是 nginx 的預設配置檔案。你也可以使用 nginx -c 指定你的配置檔案

#執行使用者
#user somebody;

#啟動程序,通常設定成和cpu的數量相等
worker_processes  1;

#全域性錯誤日誌
error_log  D:/Tools/nginx-1.10.1/logs/error.log;
error_log  D:/Tools/nginx-1.10.1/logs/notice.log  notice;
error_log  D:/Tools/nginx-1.10.1/logs/info.log  info;

#PID檔案,記錄當前啟動的nginx的程序ID
pid        D:/Tools/nginx-1.10.1/logs/nginx.pid;

#工作模式及連線數上限
events {
   worker_connections 1024;    #單個後臺worker process程序的最大併發連結數
}

#設定http伺服器,利用它的反向代理功能提供負載均衡支援
http {
   #設定mime型別(郵件支援型別),型別由mime.types檔案定義
   include       D:/Tools/nginx-1.10.1/conf/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    D:/Tools/nginx-1.10.1/logs/access.log main;
   rewrite_log     on;
   
   #sendfile 指令指定 nginx 是否呼叫 sendfile 函式(zero copy 方式)來輸出檔案,對於普通應用,
   #必須設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為 off,以平衡磁碟與網路I/O處理速度,降低系統的uptime.
   sendfile        on;
   #tcp_nopush     on;

   #連線超時時間
   keepalive_timeout  120;
   tcp_nodelay        on;
   
   #gzip壓縮開關
   #gzip  on;

   #設定實際的伺服器列表 
   upstream zp_server1{
       server 127.0.0.1:8089;
   }

   #HTTP伺服器
   server {
       #監聽80埠,80埠是知名埠號,用於HTTP協議
       listen       80;
       
       #定義使用www.xx.com訪問
       server_name  www.helloworld.com;
       
       #首頁
       index index.html
       
       #指向webapp的目錄
       root D:_WorkspaceProjectgithubzpSpringNotesspring-securityspring-shirosrcmainwebapp;
       
       #編碼格式
       charset utf-8;
       
       #代理配置引數
       proxy_connect_timeout 180;
       proxy_send_timeout 180;
       proxy_read_timeout 180;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarder-For $remote_addr;

       #反向代理的路徑(和upstream繫結),location 後面設定對映的路徑
       location / {
           proxy_pass http://zp_server1;
       } 

       #靜態檔案,nginx自己處理
       location ~ ^/(images|javascript|js|css|flash|media|static)/ {
           root D:_WorkspaceProjectgithubzpSpringNotesspring-securityspring-shirosrcmainwebappiews;
           #過期30天,靜態檔案不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設定得小一點。
           expires 30d;
       }
   
       #設定檢視Nginx狀態的地址
       location /NginxStatus {
           stub_status           on;
           access_log            on;
           auth_basic            "NginxStatus";
           auth_basic_user_file  conf/htpasswd;
       }
   
       #禁止訪問 .htxxx 檔案
       location ~ /.ht {
           deny all;
       }
       
       #錯誤處理頁面(可選擇性配置)
       #error_page   404              /404.html;
       #error_page   500 502 503 504  /50x.html;
       #location = /50x.html {
       #    root   html;
       #}
   }
}