1. 程式人生 > >Nginx快速入門

Nginx快速入門

說明:本文部分內容收集於網際網路 不完全原創 但都經過本人親身試驗


Nginx概述:

1、什麼是nginx

Nginx是一款高效能的http 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器。由俄羅斯的程式設計師Igor Sysoev所開發,官方測試nginx能夠支支撐5萬併發連結,並且cpu、記憶體等資源消耗卻非常低,執行非常穩定。

2、應用場景

http伺服器:Nginx是一個http服務可以獨立提供http服務。可以做網頁靜態伺服器

虛擬主機:可以實現在一臺伺服器虛擬出多個網站。例如個人網站使用的虛擬主機。

反向代理,負載均衡:當網站的訪問量達到一定程度後,單臺伺服器不能滿足使用者的請求時,需要用多臺伺服器叢集可以使用nginx做反向代理。並且多臺伺服器可以平均分擔負載,不會因為某臺伺服器負載高宕機而某臺伺服器閒置的情況。


Nginx安裝:

yum安裝

yum -y install nginx

 

手動安裝

要求的安裝環境

需要安裝gcc的環境。

yum install gcc-c++

第三方的開發包。

PCRE

PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 相容的正則表示式庫。nginx的http模組使用pcre來解析正則表示式,所以需要在linux上安裝pcre庫。

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。

zlib

zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。

yum install -y zlib zlib-devel

 

openssl

OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程式供測試或其它目的使用。

nginx不僅支援http協議,還支援https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。

yum install -y openssl openssl-devel

 

安裝步驟

 

第一步:把nginx的原始碼包上傳到linux系統

第二步:解壓縮

[[email protected] ~]# tar zxf nginx-1.8.0.tar.gz

第三步:使用configure命令建立一makeFile檔案。

./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

注意:啟動nginx之前,上邊將臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立tempnginx目錄

[[email protected] sbin]# mkdir /var/temp/nginx/client -p

第四步:make

第五步:make install

 

 

啟動nginx

進入sbin目錄

[[email protected] sbin]# ./nginx

 

 

關閉nginx:

[[email protected] sbin]# ./nginx -s stop

推薦使用:

[[email protected] sbin]# ./nginx -s quit

 

重啟nginx:

先關閉後啟動。

重新整理配置檔案:

[[email protected] sbin]# ./nginx -s reload

 


 

Nginx的配置檔案:

httpserver節點預設配置:

server {

    listen       80;

    server_name  localhost;



    location / {

        root   /usr/share/nginx/html;

        index  index.html index.htm;

    }



error_page  404              /404.html;



    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }



 }

SSL配置:

server
{
    listen 80 default_server;
	listen 443 ssl http2;
    server_name xxx.cn 本機ip www.xxx.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/xxx.cn;
    
    #SSL-START SSL相關配置,請勿刪除或修改下一行帶註釋的404規則
    #error_page 404/404.html;
    ssl_certificate    /etc/letsencrypt/live/xxx.cn/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/xxx.cn/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    limit_conn perserver 300;
    limit_conn perip 25;
    limit_rate 512k;
    #SSL-END
    
    #ERROR-PAGE-START  錯誤頁配置,可以註釋、刪除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以註釋或修改
    include enable-php-56.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重寫規則引用,修改後將導致面板設定的偽靜態規則失效
    include /www/server/panel/vhost/rewrite/xxx.cn.conf;
    #REWRITE-END
    
    #禁止訪問的檔案或目錄
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一鍵申請SSL證書驗證目錄相關設定
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off; 
    }
    access_log  /www/wwwlogs/xxx.cn.log;
    error_log  /www/wwwlogs/xxx.cn.error.log;
}

轉發tomcat配置

#user  nobody;
worker_processes  2;

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

#pid        logs/nginx.pid;


events {
    worker_connections  2048;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

	index index.jsp index.html;
        root /yjdata/www/www/;
        location ~ .* {
                proxy_pass http://127.0.0.1:8080;
        }


}