Nginx簡單介紹及安裝

-Nginx簡介

Nginx簡介
Nginx是一款高性能的HTTP和反向代理服務器,主要有以下優點

  • 支持高併發

  • 配置簡單

  • 佔用的資源少

nginx作爲代理服務器主要用來實現反向代理和負載均衡。
要理解反想代理首先介紹一下正向代理,如下圖正向代理是代理客戶端,服務端不知道真實的客戶端是誰,客戶端請求的服務都被代理服務器代替來請求。這裏寫圖片描述
反向代理則是代理的服務端,以代理服務器來接受Internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給Internet上請求連接的客戶端,此時代理服務器對外就表現爲一個服務器,用戶不需要也不關心到底是哪臺服務器爲自己服務,反向代理是負載均衡實現的基礎。
這裏寫圖片描述
負載均衡:簡單的理解就是將客戶端的請求分發到不同的服務器,由不同的服務器做出響應,減輕服務器的壓力。更加快速的響應客戶端的請求。

Linux環境安裝Nginx
參考文章:http://www.runoob.com/linux/nginx-install-setup.html
由於Nginx的啓動端口是80小於1024,所以使用root用戶安裝
1. 安裝編譯工具及庫文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
  1. 安裝PCRE
    PCRE 作用是讓 Nginx 支持 Rewrite 功能。
    這裏寫圖片描述

解壓安裝包

tar zxvf pcre-8.35.tar.gz

進入pcre目錄執行以下命令進行編譯安裝

cd pcre-8.35
./configure
make && make install

安裝完成之後查看PCRE的版本

pcre-config --version

這裏寫圖片描述

下載Nginx
下載地址:http://nginx.org/download/nginx-1.9.9.tar.gz

wget http://nginx.org/download/nginx-1.9.9.tar.gz

這裏寫圖片描述
解壓目錄

tar -zxvf nginx-1.9.9.tar.gz 

編譯安裝

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35

這裏寫圖片描述

make && make install

這裏寫圖片描述

到此Nginx安裝完成。
備註:nginx 常用命令
命令執行路徑在nginx/sbin目錄下

./nginx -?    //nginx命令幫助
./nginx -v    //查看nginx 版本
./nginx -t    //檢查配置文件是否正確
./nginx       //啓動nginx
./nginx -s reopen //重啓nginx
./nginx -s reload  //重新加載配置文件
./nginx -s stop  //關閉nginx

這裏寫圖片描述

啓動Nginx使用瀏覽器訪問,出現以下地址代表安裝正確。http默認端口80,如果配置文件中監聽的不是80端口,則需要加上端口號。
這裏寫圖片描述

nginx.conf 配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  warn;

#pid         logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    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"';

  log_format  main
  ' $remote_user [$time_local]  $http_x_Forwarded_for $remote_addr  $request '
  '$http_x_forwarded_for '
  '$upstream_addr '
  'ups_resp_time: $upstream_response_time '
  'request_time: $request_time' '  request_body: $request_body';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  105;

    client_max_body_size 10m;
    client_header_buffer_size  32k;
    large_client_header_buffers 4 32k;
    tcp_nopush      on;
    tcp_nodelay     on;

    #gzip  on;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_comp_level 4;
    gzip_types      text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary       on;

    proxy_buffers   8 8k;

    proxy_cache_path     data/cache levels=1:2 keys_zone=one:10m;
    proxy_cache_min_uses 4;
    proxy_cache_valid    10m;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    fastcgi_buffers 8 128k;

    server_tokens off;

    server {

    listen 80;
        server_name localhost;

    location / {
          rewrite ^/(.*) https://$host/$1;
           }
        location /mweb {
            proxy_pass http://222.85.230.6:8080/mweb;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_ignore_client_abort on;

            client_max_body_size    10m;
            client_body_buffer_size 128k;
        }



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

            client_max_body_size    10m;
            client_body_buffer_size 128k;
        }

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

            client_max_body_size    10m;
            client_body_buffer_size 128k;
        }



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

            client_max_body_size    10m;
            client_body_buffer_size 128k;
        }

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

            client_max_body_size    10m;
            client_body_buffer_size 128k;
        }
        location /mercStandard {
                proxy_pass  http://222.85.230.6:8080/mercStandard;
        }


        location /sysmng { 
             deny all; 
        } 

        location /console {
            deny  all;
        }

        location /admin-console {
            deny  all;
        }

        location /management {
            deny  all;
        }

        location /jmx-console {
            deny  all;
        }

        location /invoker {
            deny  all;
        }

        location /web-console {
            deny  all;
        }

    #   location /status {
    #       check_status;
    #       access_log off;
    #       allow 100.126.54.0/24;
    #       deny  all;
    #        }

        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
     }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章