Nginx安裝說明

jopen 9年前發布 | 13K 次閱讀 Nginx Web服務器

1.介紹

      Nginx 是一個很強大的高性能Web和反向代理服務器,它具有很多非常優越的特性  

2.安裝

     2.1 下載

     下載地址:http://nginx.org/en/download.html

      Windows: nginx-1.6.2.zip

      Unix: nginx-1.6.2.tar.gz      

     2.2 解壓

         tar zxvf    nginx-1.6.2.tar.gz  [-C {解壓目錄}]

     2.3 配置

        cd   nginx-1.6.2

       ./configure --prefix={安裝目錄} --with-http_stub_status_module --without-http_rewrite_module --without-http_gzip_module

       make    

       make install

      2.4 檢測

          檢查是否安裝成功      

          進入安裝目錄

         ./sbin/nginx -t 

         如果安裝成功結果顯示如下:

        nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

        nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

      2.5 啟動 

           ./sbin/nginx       

          ie 瀏覽器中輸入 http://{目標IP}

        注意,這里nginx監聽80端口,所以要在iptables里打開80端口。

        /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

        接著訪問這臺機器的80的端口,如果請求成功,則說明配置成功。

       為了操作方便,可以自己寫一個nginx命令腳本,放到/etc/init.d下,執行方法如下:

啟動:service nginx start

停止:service nginx stop

重啟:service nginx reconfigure

查看狀態:service nginx status


   3.常見問題

   3.1 .安裝Nginx時報錯 ./configure: error: the HTTP rewrite module requires the PCRE library.

           安裝pcre-devel解決問題   yum -y install pcre-devel

  4.配置

4.1 基本配置方法

location / {
               proxy_pass http://127.0.0.1:8080;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ^~ /api/ {
               proxy_pass http://127.0.0.1:8081;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }


4.2 負載均衡配置

upstream backend {
         #ip_hash;
         server 127.0.0.1:8081;
         server 10.10.136.85:8082;
}    
upstream backendweb {
          ip_hash;
          server 127.0.0.1:8080;
          server 10.10.136.85:8081;
}

    server {
        listen       80;
        server_name  www.nfzo.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
               #proxy_pass http://127.0.0.1:8080;
               proxy_pass http://backendweb;
               proxy_read_timeout 300;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ^~ /api/ {
               #proxy_pass http://127.0.0.1:8081;
               proxy_pass http://backend;
               proxy_read_timeout 300;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }



4.3  啟用nginx status配置

在默認主機里面加上location或者你希望能訪問到的主機里面。

server {
    listen  *:80 default_server;
    server_name _;
    location /ngx_status
    {
        stub_status on;
        access_log off;
        #allow 127.0.0.1;
        #deny all;
    }
}


 打開status頁面

# curl http://127.0.0.1/ngx_status
Active connections: 11921
server accepts handled requests
 11989 11989 11991
Reading: 0 Writing: 7 Waiting: 42

 nginx status詳解:


  1. active connections – 活躍的連接數量
  2. server accepts handled requests — 總共處理了11989個連接 , 成功創建11989次握手, 總共處理了11991個請求
  3. reading — 讀取客戶端的連接數.
  4. writing — 響應數據到客戶端的數量
  5. waiting — 開啟 keep-alive 的情況下,這個值等于 active – (reading+writing), 意思就是 Nginx 已經處理完正在等候下一次請求指令的駐留連接.


4.4 僅限域名訪問配置

      這個可以配置server 來限定,比如:

   

 server {
        listen 80;
        server_name .*;

        location / {
            return 403;
        }

   }

    server {
        listen       80;
        server_name  qimdev.qiyi.domain;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;  location / {
               proxy_pass http://127.0.0.1:8080;
               proxy_set_header Host $host;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }


4.5 設置靜態文件

如果不是在默認目錄下,使用alias配置

location ^~ /project/ {
              alias /home/qiyi/data/www/;
              index  index.html index.htm;
              #charset GBK;
              #expires      7d;
     }


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!