nginx配置

編輯本隨筆php

Nginx

ebookhtml

刪除帶#的行nginx

sed -i "/#/d" nginx.confweb

有空行的也刪除json

sed -i "/^$/d" nginx.conf後端

中文文檔緩存

官網下載地址服務器

安裝:

mv nginx-1.15.5.tar.gz /usr/src/
cd /usr/src/
tar -zxvf nginx-1.15.5.tar.gz
cd nginx-1.15.5/

安裝環境:yum install gcc pcre-devel zlib zlib-develsession

檢查環境,是否知足安裝條件以及依賴解決多線程

指定軟件安裝位置./configure --prefix=/usr/local/nginx

目錄:

各個文件以及目錄

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

啓動Nginx服務:

驗證:

lsof -i :80

netstat -ntpl

建立用戶

useradd -s /sbin/nologin /r www

nginx從新加載配置文件

killall -s HUP nginx

目錄訪問控制

#配置目錄訪問
#相對網站根目錄,/即root目錄,root目錄爲/usr/local/nginx/html
#訪問控制,只容許本機訪問a目錄,其餘機器拒接
location /a {
    allow 127.0.0.1;
    deny all;

    #設置返回
    return 404;
}

建立用戶登陸密碼文件

工具來源於httpd-tools

[root@web01 html]# htpasswd -c /etc/nginx/htpasswd user1
New password:
Re-type new password:
Adding password for user user1
[root@web01 html]#

只有擁有用戶名和密碼的用戶才能訪問

#目錄用戶驗證:任何人均可以訪問,但須要提供用戶名和密碼
location /b {
    auth_basic "登錄驗證";
    auth_basic_user_file /etc/nginx/htpasswd;
}

放盜鏈配置

放置其餘站點鏈接本網站上的數據,在server中配置

#針對全局作防盜連配置
#location ~* \.(png|gif|bmp)$
#針對c文件夾作防盜連配置
location /c {
    #正常匹配,none標示沒有,blocked標示防火牆,域名標示指定域名
    valid_referers none blocked *.self.com;
            
    #沒有匹配以上,則直接返回403
    if ($invalid_referer){
        return 403;
    }   
}

虛擬主機

一個web服務器軟件默認狀況下只能發佈一個web,由於web發佈須要三個條件(ip,port,domainName)

虛擬主機:把一臺物理服務器劃分爲多個「虛擬」的服務器,每個虛擬主機均可以有獨立的域名和獨立的目錄

基於IP

  1. 須要多個ip
  2. DocumentRoot必須存在
http {
    server {
        listen       10.111.1.31:80;
        server_name  www.web.com;
        location / {
            root   html/web1;
            index  index.html index.htm;
        }
    }

    server {
        listen       10.111.1.32:80;
        server_name  www.web.com;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
}

基於端口

http {
    server {
        listen       10.111.1.32:80;
        location / {
            root   html/web1;
            index  index.html index.htm;
        }
    }

    server {
        listen       10.111.1.32:8080;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
}

基於域名

一個網站必須有一個域名

http {
    server {
        listen       80;
        server_name  www.abc.com;
        location / {
            root   html/web1;
            index  index.html index.htm;
        }
    }

    server {
        listen       80;
        server_name  www.cbd.com;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
}

完整配置文件

#全局配置,指定用戶組,pid存放位置,日誌路徑,worker process數量

#啓動nginx子進程默認用戶
#user  nobody;

#最多啓動的子進程(工做進程,單進程多線程)數量,通常數量爲CPU內核數
worker_processes  1;

#全局錯誤日誌的位置以及日誌格式
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#主進程號存放位置
#pid        logs/nginx.pid;


events {
    #配置用戶與服務器鏈接相關配置,如進程鏈接數,事件驅動模型,鏈接序列化等
    #每一個工做進程最大併發數
    worker_connections  1024;
}


#http服務器設置
http {
    #嵌套多個server,配置代理,緩存,日誌,三方模塊等
    #設定mime類型,類型由mime.type文件定義
    include       mime.types;
    default_type  application/octet-stream;

    #自定義日誌格式
    log_format baism_01 '[$time_local] $remote_addr "$request" $status';

    #定義json格式的日誌
    log_format main_json '{
        "timestamp":"$time_local",
        "client_ip":"$remote_addr",
        "status":"$status",
        }';


    #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;

    #指定nginx是否調用sendfile函數來輸出文件,對於普通應用必須爲on
    sendfile        on;
        \
    #tcp_nopush     on;

    #長鏈接超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #壓縮輸出
    #gzip  on;
    
        
    #一個server發佈一個網站,即一個server就是一個虛擬主機,只有一個server的時候就是默認網站
    server {

        #網站監聽端口
        listen       80;

        #網站域名
        server_name  localhost;

        #虛擬機支持的字符集
        #charset utf-8;

        #虛擬主機的訪問日誌路徑
        #access_log  logs/host.access.log  main;

        #使用自定義日誌格式
        access_log logs/baism_01.access.log baism_01;

        #定義web根路徑
        location / {
            #請求路由以及各頁面處理狀況
            root   html;
            index  index.html index.htm;
        }

        #配置目錄訪問
        #相對網站根目錄,/即root目錄,root目錄爲/usr/local/nginx/html
        #訪問控制,只容許本機訪問a目錄,其餘機器拒接
        location /a {
            allow 127.0.0.1;
            deny all;

            #設置返回
            return 404;
        }


        #目錄用戶驗證:任何人均可以訪問,但須要提供用戶名和密碼
        location /b {
            auth_basic "登錄驗證";
            auth_basic_user_file /etc/nginx/htpasswd;
        }

        
        #針對全局作防盜連配置
        #location ~* \.(png|gif|bmp)$
        #針對c文件夾作防盜連配置
        location /c {
            #正常匹配,none標示沒有,blocked標示防火牆,域名標示指定域名
            valid_referers none blocked *.self.com;
            
            #沒有匹配以上,則直接返回403
            if ($invalid_referer){
                return 403;
            }   
        }

"nginx.conf" 189L, 5059C written
[root@web01 conf]# cat nginx.conf
#全局配置,指定用戶組,pid存放位置,日誌路徑,worker process數量

#啓動nginx子進程默認用戶
#user  nobody;

#最多啓動的子進程(工做進程,單進程多線程)數量,通常數量爲CPU內核數
worker_processes  1;

#全局錯誤日誌的位置以及日誌格式
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#主進程號存放位置
#pid        logs/nginx.pid;


events {
    #配置用戶與服務器鏈接相關配置,如進程鏈接數,事件驅動模型,鏈接序列化等
    #每一個工做進程最大併發數
    worker_connections  1024;
}


#http服務器設置
http {
    #嵌套多個server,配置代理,緩存,日誌,三方模塊等
    #設定mime類型,類型由mime.type文件定義
    include       mime.types;
    default_type  application/octet-stream;

    #自定義日誌格式
    log_format baism_01 '[$time_local] $remote_addr "$request" $status';

    #定義json格式的日誌
    log_format main_json '{
        "timestamp":"$time_local",
        "client_ip":"$remote_addr",
        "status":"$status",
        }';


    #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;

    #指定nginx是否調用sendfile函數來輸出文件,對於普通應用必須爲on
    sendfile        on;
    #tcp_nopush     on;

    #長鏈接超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #壓縮輸出
    #gzip  on;
    
    #一個server發佈一個網站,即一個server就是一個虛擬主機,只有一個server的時候就是默認網站
    server {

        #網站監聽端口
        listen       80;

        #網站域名
        server_name  localhost;

        #虛擬機支持的字符集
        #charset utf-8;

        #虛擬主機的訪問日誌路徑
        #access_log  logs/host.access.log  main;

        #使用自定義日誌格式
        access_log logs/baism_01.access.log baism_01;

        #定義web根路徑
        location / {
            #請求路由以及各頁面處理狀況
            root   html;
            index  index.html index.htm;
        }

        #配置目錄訪問
        #相對網站根目錄,/即root目錄,root目錄爲/usr/local/nginx/html
        #訪問控制,只容許本機訪問a目錄,其餘機器拒接
        location /a {
            allow 127.0.0.1;
            deny all;
            
            #設置返回
            return 404;
        }


        #目錄用戶驗證:任何人均可以訪問,但須要提供用戶名和密碼
        location /b {
            auth_basic "登錄驗證";
            auth_basic_user_file /etc/nginx/htpasswd;
        }


        #針對全局作防盜連配置
        #location ~* \.(png|gif|bmp)$
        #針對c文件夾作防盜連配置
        location /c {
            #正常匹配,none標示沒有,blocked標示防火牆,域名標示指定域名
            valid_referers none blocked *.self.com;
            
            #沒有匹配以上,則直接返回403
            if ($invalid_referer){
                return 403;
            }
        }


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #錯誤碼返回錯誤頁面,路徑相對於localtion路徑
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        #定義反向代理服務器,數據服務器由其餘服務器提供
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
View Code

Nginx反向代理

常規代理服務器優化

代理用戶正向代理,代理服務器即爲反向代理

堡壘機場景

只有一個IP,發佈內部多臺服務器場景

緩存場景,靜態緩存服務器,加載快,後端服務器壓力減少

location / {
   proxy_pass   http://127.0.0.1;
  #添加代理主機頭,添加後後端服務器能夠經過相應字段提取
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }

限速

給定時間段內可以產生的HTTP請求數

限制請求速率,通常限流是用在保護上有應用服務器不被在同一時刻的大量用戶請求湮滅

放置DDOS攻擊

保護IO

案例一:

案例二:

相關文章
相關標籤/搜索