日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

70、Nginx配置實(shí)戰(zhàn)

 印度阿三17 2019-02-13

正常運(yùn)行的必備配置:

1、user username [groupname];

指定運(yùn)行worker進(jìn)程的用戶和組

2、pid /path/to/pidfile_name;

指定nginx的pid文件

3、worker_rlimit_nofile #;

指定一個(gè)worker進(jìn)程所能夠打開的最大文件句柄數(shù);

4、worker_rlimit_sigpending #;

設(shè)定每個(gè)用戶能夠發(fā)往worker進(jìn)程的信號(hào)的數(shù)量;


優(yōu)化性能相關(guān)的配置:

1、worker_processes #;

worker進(jìn)程的個(gè)數(shù);通常其數(shù)值應(yīng)該為CPU的物理核心數(shù)減1;

2、worker_cpu_affinity cpumask ...;

指定worker只運(yùn)行在哪顆CPU上,運(yùn)行在哪顆CPU上,對(duì)應(yīng)位為1。

worker_processes 6;?

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000;?

3、ssl_engine device;

在存在ssl硬件加速器的服務(wù)器上,指定所使用的ssl硬件加速設(shè)備;

4、timer_resolution t;

每次內(nèi)核事件調(diào)用返回時(shí),都會(huì)使用gettimeofday()來(lái)更新nginx緩存時(shí)鐘;timer_resolution用于定義每隔多久才會(huì)由gettimeofday()更新一次緩存時(shí)鐘;x86-64系統(tǒng)上,gettimeofday()代價(jià)已經(jīng)很小,可以忽略此配置;

5、worker_priority nice;

-20,19之間的值;


事件相關(guān)的配置

1、accept_mutex [on|off]

是否打開Ningx的負(fù)載均衡鎖;此鎖能夠讓多個(gè)worker輪流地、序列化地與新的客戶端建立連接;而通常當(dāng)一個(gè)worker進(jìn)程的負(fù)載達(dá)到其上限的7/8,master就盡可能不再將請(qǐng)求調(diào)度此worker;

2、lock_file /path/to/lock_file;?

lock文件

3、accept_mutex_delay #ms;

accept鎖模式中,一個(gè)worker進(jìn)程為取得accept鎖的等待時(shí)長(zhǎng);如果某worker進(jìn)程在某次試圖取得鎖時(shí)失敗了,至少要等待#ms才能再一次請(qǐng)求鎖;

4、multi_accept on|off;

是否允許一次性地響應(yīng)多個(gè)用戶請(qǐng)求;默認(rèn)為Off;?

5、use [epoll|rtsig|select|poll];

定義使用的事件模型,建議讓nginx自動(dòng)選擇;

6、worker_connections #;

每個(gè)worker能夠并發(fā)響應(yīng)最大請(qǐng)求數(shù);


用于調(diào)試、定位問題: 只調(diào)試nginx時(shí)使用

1、daemon on|off;

是否讓ningx運(yùn)行于后臺(tái);默認(rèn)為on,調(diào)試時(shí)可以設(shè)置為off,使得所有信息直接輸出到控制臺(tái);

2、master_process on|off

是否以master/worker模式運(yùn)行nginx;默認(rèn)為on;調(diào)試時(shí)可設(shè)置off以方便追蹤;

3、error_log /path/to/error_log level;

錯(cuò)誤日志文件及其級(jí)別;默認(rèn)為error級(jí)別;調(diào)試時(shí)可以使用debug級(jí)別,但要求在編譯時(shí)必須使用--with-debug啟用debug功能;


nginx的http web功能:

必須使用虛擬機(jī)來(lái)配置站點(diǎn);每個(gè)虛擬主機(jī)使用一個(gè)server {}段配置;非虛擬主機(jī)的配置或公共配置,需要定義在server之外,http之內(nèi);

http {

directive value;

...


server {


}

server {


}

...

}


虛擬主機(jī)相關(guān)的配置:

1、server {}

定義一個(gè)虛擬主機(jī);nginx支持使用基于主機(jī)名或IP的虛擬主機(jī);

2、listen?

listen address[:port];

listen port?


default_server:定義此server為http中默認(rèn)的server;如果所有的server中沒有任何一個(gè)listen使用此參數(shù),那么第一個(gè)server即為默認(rèn)server;?

rcvbuf=SIZE: 接收緩沖大??;

sndbuf=SIZE: 發(fā)送緩沖大?。?/p>

ssl: https server;

3、server_name [...];

server_name可以跟多個(gè)主機(jī)名,名稱中可以使用通配符和正則表達(dá)式(通常以~開頭);當(dāng)nginx收到一個(gè)請(qǐng)求時(shí),會(huì)取出其首部的server的值,而后跟眾server_name進(jìn)行比較;比較方式:

(1) 先做精確匹配;www.?

(2) 左側(cè)通配符匹配;*.

(3) 右側(cè)通配符匹配;www.abc.com, www.*

(4) 正則表達(dá)式匹配: ~^.*\.nginxtest\.com$

4、server_name_hash_bucket_size 32|64|128;

為了實(shí)現(xiàn)快速主機(jī)查找,nginx使用hash表來(lái)保存主機(jī)名;

5、location [ = | ~ | ~* | ^~ ] uri { ... }

? ?location @name { ... }

? ?功能:允許根據(jù)用戶請(qǐng)求的URI來(lái)匹配指定的各location以進(jìn)行訪問配置;匹配到時(shí),將被location塊中的配置所處理;比如:http://www./images/logo.gif


? ?=:精確匹配;

? ?~:正則表達(dá)式模式匹配,匹配時(shí)區(qū)分字符大小寫

? ?~*:正則表達(dá)式模式匹配,匹配時(shí)忽略字符大小寫

? ?^~: URI前半部分匹配,不檢查正則表達(dá)式

? ?

匹配優(yōu)先級(jí):

字符字面量最精確匹配、正則表達(dá)式檢索(由第一個(gè)匹配到所處理)、按字符字面量


文件路徑定義:

1、root path

設(shè)置web資源路徑;用于指定請(qǐng)求的根文檔目錄;

location / {

root /www/htdocs;

}


location ^~ /images/ {

root /web;

}

2、alias path

只能用于location中,用于路徑別名;

location / {

root /www/htdocs;

}


location ^~ /images/ {

alias /web;

}

3、index file ...;

定義默認(rèn)頁(yè)面,可參跟多個(gè)值;

4、error_page code ... [=[response]] uri;

當(dāng)對(duì)于某個(gè)請(qǐng)求返回錯(cuò)誤時(shí),如果匹配上了error_page指令中設(shè)定的code,則重定向到新的URI中。

錯(cuò)誤頁(yè)面重定向;

5、try_files path1 [path2 ...] uri;

自左至右嘗試讀取由path所指定路徑,在第一次找到即停止并返回;如果所有path均不存在,則返回最后一個(gè)uri;?


? ? ? ? location ~* ^/documents/(.*)$ {

? ? ? ? ? ? root /www/htdocs;

? ? ? ? ? ? try_files $uri /docu/$1 /temp.html;

? ? ? ? }

? ? ? ??

網(wǎng)絡(luò)連接相關(guān)的設(shè)置:

1、keepalive_timeout time;

保持連接的超時(shí)時(shí)長(zhǎng);默認(rèn)為75秒;

2、keepalive_requests n;

在一次長(zhǎng)連接上允許承載的最大請(qǐng)求數(shù);

3、keepalive_disable [msie6 | safari | none ]

對(duì)指定的瀏覽器禁止使用長(zhǎng)連接;

4、tcp_nodelay on|off

對(duì)keepalive連接是否使用TCP_NODELAY選項(xiàng);

5、client_header_timeout time;?

讀取http請(qǐng)求首部的超時(shí)時(shí)長(zhǎng);

6、client_body_timeout time;

讀取http請(qǐng)求包體的超時(shí)時(shí)長(zhǎng);

7、send_timeout time;

發(fā)送響應(yīng)的超時(shí)時(shí)長(zhǎng);


對(duì)客戶端請(qǐng)求的限制:

1、limit_except method ... { ... }

指定對(duì)范圍之外的其它方法的訪問控制;

limit_except GET {

allow 172.16.0.0/16;

deny all;?

}

2、client_max_body_size SIZE;

http請(qǐng)求包體的最大值;常用于限定客戶所能夠請(qǐng)求的最大包體;根據(jù)請(qǐng)求首部中的Content-Length來(lái)檢測(cè),以避免無(wú)用的傳輸;

3、limit_rate speed;

限制客戶端每秒鐘傳輸?shù)淖止?jié)數(shù);默認(rèn)為0,表示沒有限制;

4、limit_rate_after time;

nginx向客戶發(fā)送響應(yīng)報(bào)文時(shí),如果時(shí)長(zhǎng)超出了此處指定的時(shí)長(zhǎng),則后續(xù)的發(fā)送過(guò)程開始限速;


文件操作的優(yōu)化:

1、sendfile on|off

是否啟用sendfile功能;

2、aio on|off

是否啟用aio功能;

3、open_file_cache max=N [inactive=time]|off

是否打開文件緩存功能;

max: 緩存條目的最大值;當(dāng)滿了以后將根據(jù)LRU算法進(jìn)行置換;

inactive: 某緩存條目在指定時(shí)長(zhǎng)時(shí)沒有被訪問過(guò)時(shí),將自動(dòng)被刪除;默認(rèn)為60s;?


緩存的信息包括:

文件句柄、文件大小和上次修改時(shí)間;

已經(jīng)打開的目錄結(jié)構(gòu);

沒有找到或沒有訪問權(quán)限的信息;

4、open_file_cache_errors on|off

是否緩存文件找不到或沒有權(quán)限訪問等相關(guān)信息;

5、open_file_cache_valid time;

多長(zhǎng)時(shí)間檢查一次緩存中的條目是否超出非活動(dòng)時(shí)長(zhǎng),默認(rèn)為60s;?

6、open_file_cache_min_use #;

在inactive指定的時(shí)長(zhǎng)內(nèi)被訪問超此處指定的次數(shù)地,才不會(huì)被刪除;


對(duì)客戶端請(qǐng)求的特殊處理:

1、ignore_invalid_headers on|off

是否忽略不合法的http首部;默認(rèn)為on; off意味著請(qǐng)求首部中出現(xiàn)不合規(guī)的首部將拒絕響應(yīng);只能用于server和http;?

2、log_not_found on|off

是否將文件找不到的信息也記錄進(jìn)錯(cuò)誤日志中;

3、resolver address;

指定nginx使用的dns服務(wù)器地址;

4、resover_timeout time;

指定DNS解析超時(shí)時(shí)長(zhǎng),默認(rèn)為30s;?

5、server_tokens on|off;

是否在錯(cuò)誤頁(yè)面中顯示nginx的版本號(hào);


內(nèi)存及磁盤資源分配:

1、client_body_in_file_only on|clean|off

HTTP的包體是否存儲(chǔ)在磁盤文件中;非off表示存儲(chǔ),即使包體大小為0也會(huì)創(chuàng)建一個(gè)磁盤文件;on表示請(qǐng)求結(jié)束后包體文件不會(huì)被刪除,clean表示會(huì)被刪除;

2、client_body_in_single_buffer on|off;

HTTP的包體是否存儲(chǔ)在內(nèi)存buffer當(dāng)中;默認(rèn)為off;

3、cleint_body_buffer_size size;

nginx接收HTTP包體的內(nèi)存緩沖區(qū)大??;

4、client_body_temp_path dir-path [level1 [level2 [level3]]];

HTTP包體存放的臨時(shí)目錄;

5、client_header_buffer_size size;

正常情況下接收用戶請(qǐng)求的http報(bào)文header部分時(shí)分配的buffer大??;默認(rèn)為1k;

6、large_client_header_buffers number size;?

存儲(chǔ)超大Http請(qǐng)求首部的內(nèi)存buffer大小及個(gè)數(shù);

7、connection_pool_size size;

nginx對(duì)于每個(gè)建立成功的tcp連接都會(huì)預(yù)先分配一個(gè)內(nèi)存池,此處即用于設(shè)定此內(nèi)存池的初始大??;默認(rèn)為256;

8、request_pool_size size;

nginx在處理每個(gè)http請(qǐng)求時(shí)會(huì)預(yù)先分配一個(gè)內(nèi)存池,此處即用于設(shè)定此內(nèi)存池的初始大?。荒J(rèn)為4k;?


http核心模塊的內(nèi)置變量:

$uri: 當(dāng)前請(qǐng)求的uri,不帶參數(shù);

$request_uri: 請(qǐng)求的uri,帶完整參數(shù);

$host: http請(qǐng)求報(bào)文中host首部;如果請(qǐng)求中沒有host首部,則以處理此請(qǐng)求的虛擬主機(jī)的主機(jī)名代替;

$hostname: nginx服務(wù)運(yùn)行在的主機(jī)的主機(jī)名;

$remote_addr: 客戶端IP

$remote_port: 客戶端Port

$remote_user: 使用用戶認(rèn)證時(shí)客戶端用戶輸入的用戶名;

$request_filename: 用戶請(qǐng)求中的URI經(jīng)過(guò)本地root或alias轉(zhuǎn)換后映射的本地的文件路徑;

$request_method: 請(qǐng)求方法

$server_addr: 服務(wù)器地址

$server_name: 服務(wù)器名稱

$server_port: 服務(wù)器端口

$server_protocol: 服務(wù)器向客戶端發(fā)送響應(yīng)時(shí)的協(xié)議,如http/1.1, http/1.0

$scheme: 在請(qǐng)求中使用scheme, 如https://www./中的https;

$http_HEADER: 匹配請(qǐng)求報(bào)文中指定的HEADER,$http_host匹配請(qǐng)求報(bào)文中的host首部

$sent_http_HEADER: 匹配響應(yīng)報(bào)文中指定的HEADER,例如$http_content_type匹配響應(yīng)報(bào)文中的content-type首部;

$document_root:當(dāng)前請(qǐng)求映射到的root配置;


1、安裝nginx

groupadd -r nginx

useradd -r -g nginx nginx

yum -y groupinstall "Development Tools"

yum -y install openssl-devel pcre-devel

wget http:///download/nginx-1.15.8.tar.gz

tar zxvf nginx-1.15.8.tar.gz?

cd nginx-1.15.8

./configure \

? --prefix=/usr \

? --sbin-path=/usr/sbin/nginx \

? --conf-path=/etc/nginx/nginx.conf \

? --error-log-path=/var/log/nginx/error.log \

? --http-log-path=/var/log/nginx/access.log \

? --pid-path=/var/run/nginx/nginx.pid? \

? --lock-path=/var/lock/nginx.lock \

? --user=nginx \

? --group=nginx \

? --with-http_ssl_module \

? --with-http_flv_module \

? --with-http_stub_status_module \

? --with-http_gzip_static_module \

? --http-client-body-temp-path=/var/tmp/nginx/client/ \

? --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

? --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

? --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

? --http-scgi-temp-path=/var/tmp/nginx/scgi \

? --with-pcre \

--with-http_addition_module?

make && make install



為nginx提供SysV init腳本:

新建文件/etc/rc.d/init.d/nginx,內(nèi)容如下:

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:? ?- 85 15?

# description:? Nginx is an HTTP(S) server, HTTP(S) reverse \

#? ? ? ? ? ? ? ?proxy and IMAP/POP3 proxy server

# processname: nginx

# config:? ? ? /etc/nginx/nginx.conf

# config:? ? ? /etc/sysconfig/nginx

# pidfile:? ? ?/var/run/nginx.pid

?

# Source function library.

. /etc/rc.d/init.d/functions

?

# Source networking configuration.

. /etc/sysconfig/network

?

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

?

nginx="/usr/sbin/nginx"

prog=$(basename $nginx)

?

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

?

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

?

lockfile=/var/lock/subsys/nginx

?

make_dirs() {

? ?# make required directories

? ?user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

? ?options=`$nginx -V 2>&1 | grep 'configure arguments:'`

? ?for opt in $options; do

? ? ? ?if [ `echo $opt | grep '.*-temp-path'` ]; then

? ? ? ? ? ?value=`echo $opt | cut -d "=" -f 2`

? ? ? ? ? ?if [ ! -d "$value" ]; then

? ? ? ? ? ? ? ?# echo "creating" $value

? ? ? ? ? ? ? ?mkdir -p $value && chown -R $user $value

? ? ? ? ? ?fi

? ? ? ?fi

? ?done

}

?

start() {

? ? [ -x $nginx ] || exit 5

? ? [ -f $NGINX_CONF_FILE ] || exit 6

? ? make_dirs

? ? echo -n $"Starting $prog: "

? ? daemon $nginx -c $NGINX_CONF_FILE

? ? retval=$?

? ? echo

? ? [ $retval -eq 0 ] && touch $lockfile

? ? return $retval

}

?

stop() {

? ? echo -n $"Stopping $prog: "

? ? killproc $prog -QUIT

? ? retval=$?

? ? echo

? ? [ $retval -eq 0 ] && rm -f $lockfile

? ? return $retval

}

?

restart() {

? ? configtest || return $?

? ? stop

? ? sleep 1

? ? start

}

?

reload() {

? ? configtest || return $?

? ? echo -n $"Reloading $prog: "

? ? killproc $nginx -HUP

? ? RETVAL=$?

? ? echo

}

?

force_reload() {

? ? restart

}

?

configtest() {

? $nginx -t -c $NGINX_CONF_FILE

}

?

rh_status() {

? ? status $prog

}

?

rh_status_q() {

? ? rh_status >/dev/null 2>&1

}

?

case "$1" in

? ? start)

? ? ? ? rh_status_q && exit 0

? ? ? ? $1

? ? ? ? ;;

? ? stop)

? ? ? ? rh_status_q || exit 0

? ? ? ? $1

? ? ? ? ;;

? ? restart|configtest)

? ? ? ? $1

? ? ? ? ;;

? ? reload)

? ? ? ? rh_status_q || exit 7

? ? ? ? $1

? ? ? ? ;;

? ? force-reload)

? ? ? ? force_reload

? ? ? ? ;;

? ? status)

? ? ? ? rh_status

? ? ? ? ;;

? ? condrestart|try-restart)

? ? ? ? rh_status_q || exit 0

? ? ? ? ? ? ;;

? ? *)

? ? ? ? echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

? ? ? ? exit 2

esac


chmod x /etc/rc.d/init.d/nginx

chkconfig --add nginx

chkconfig nginx on


service nginx start



1、配置虛擬主機(jī)

grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?80;

? ? ? ? server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? ?html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? ? ? error_page? ?500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? ?html;

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? root /var/www/html;

? ? }

}


2、配置訪問控制(只有允許192.168.130.53網(wǎng)段訪問)

grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?80;

? ? ? ? server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? ?html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? ? ? error_page? ?500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? ?html;

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? root /var/www/html;

? ? ? ? allow 192.168.53.0/24;

? ? ? ? deny all;

? ? }

}


3、配置用戶認(rèn)證(訪問www./admin會(huì)彈出認(rèn)證界面)

grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?80;

? ? ? ? server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? ?html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? ? ? error_page? ?500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? ?html;

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? root /var/www/html;

? ? ? ? allow 192.168.53.0/24;

? ? ? ? deny all;


? ? ? ? location /admin/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? auth_basic "admin_area";

? ? ? ? ? ? auth_basic_user_file /etc/nginx/.htpasswd;

? ? ? ? }

? ? }

}


htpasswd -c -m /etc/nginx/.htpasswd test1

htpasswd -m /etc/nginx/.htpasswd test2


echo "<h1>admin</h1>" >> /var/www/html/a/admin/index.html


4、通過(guò)authoindex配置下載站點(diǎn)

編譯的時(shí)候需要加 --with-http_addition_module

需要將authoindex.html放到根目錄/var/www/html下

https://raw./phuslu/phuslu.github.io/master/autoindex.html


grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?80;

? ? ? ? server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? ?html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? ? ? error_page? ?500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? ?html;

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? root /var/www/html;

? ? ? ? allow 192.168.0.0/16;

? ? ? ? deny all;


? ? ? ? location /admin/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? auth_basic "admin_area";

? ? ? ? ? ? auth_basic_user_file /etc/nginx/.htpasswd;

? ? ? ? }


? ? ? ? location /download/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? autoindex on;

? ? ? ? ? ? autoindex_exact_size off;

? ? ? ? ? ? autoindex_localtime on;

? ? ? ? ? ? charset utf-8;

? ? ? ? ? ? add_after_body /autoindex.html;

? ? ? ? }

? ? }

}


5、配置防盜鏈

(1) 定義合規(guī)的引用

valid_referers none | blocked | server_names | string ...;


(2) 拒絕不合規(guī)的引用

if? ($invalid_referer) {

rewrite ^/.*$ http://www./403.html?

}?


防止其他網(wǎng)站套用www.的圖片文件


grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?80;

? ? ? ? server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? ?html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? ? ? error_page? ?500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? ?html;

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? server_name 192.168.60.12;

? ? ? ? root /var/www/html;

? ? ? ? allow 192.168.0.0/16;

? ? ? ? deny all;


? ? ? ? location /admin/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? auth_basic "admin_area";

? ? ? ? ? ? auth_basic_user_file /etc/nginx/.htpasswd;

? ? ? ? }


? ? ? ? location /download/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? autoindex on;

? ? ? ? ? ? autoindex_exact_size off;

? ? ? ? ? ? autoindex_localtime on;

? ? ? ? ? ? charset utf-8;

? ? ? ? ? ? add_after_body /autoindex.html;

? ? ? ? }


? ? ? ? location ~*\.(jpg|png|gif|jpeg)$ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? valid_referers none blocked www. *.;

? ? ? ? ? ? if ($invalid_referer) {

? ? ? ? ? ? ? ? #rewrite ^/ http://www./403.html;

? ? ? ? ? ? ? ? return 404;

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen 80;

? ? ? ? server_name www.b.com;

? ? ? ? root /var/www/html/b;

? ? }

}

? ? ? ??

cat /var/www/html/index.html?

<h1>www.</h1>

<img src="http://www./images/1.png">


cat /var/www/html/b/index.html?

<h1>www.b.com</h1>

<img src="http://www./images/1.png">



6、配置URL rewrite

rewrite regex replacement [flag];


last: 一旦被當(dāng)前規(guī)則匹配并重寫后立即停止檢查后續(xù)的其它rewrite的規(guī)則,而后通過(guò)重寫后的規(guī)則重新發(fā)起請(qǐng)求;

break: 一旦被當(dāng)前規(guī)則匹配并重寫后立即停止后續(xù)的其它rewrite的規(guī)則,而后繼續(xù)由nginx進(jìn)行后續(xù)操作;

redirect: 返回302臨時(shí)重定向;

permanent: 返回301永久重定向;


nginx最多循環(huán)10次,超出之后會(huì)返回500錯(cuò)誤;

注意:一般將rewrite寫在location中時(shí)都使用break標(biāo)志,或者將rewrite寫在if上下文中;


當(dāng)訪問到download目錄下的jpg、gif、jpeg、png時(shí)自動(dòng)跳轉(zhuǎn)到images目錄下


grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?80;

? ? ? ? server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? ?html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? ? ? error_page? ?500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? ?html;

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? server_name 192.168.60.12;

? ? ? ? root /var/www/html;

? ? ? ? allow 192.168.0.0/16;

? ? ? ? deny all;


? ? ? ? location /admin/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? auth_basic "admin_area";

? ? ? ? ? ? auth_basic_user_file /etc/nginx/.htpasswd;

? ? ? ? }


? ? ? ? location /download/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? autoindex on;

? ? ? ? ? ? autoindex_exact_size off;

? ? ? ? ? ? autoindex_localtime on;

? ? ? ? ? ? charset utf-8;

? ? ? ? ? ? add_after_body /autoindex.html;

? ? ? ? ? ? rewrite ^/download/(.*\.(jpg|gif|jpeg|png))$ /images/$1 break;

? ? ? ? }


? ? }

}??




7、配置重寫記錄到錯(cuò)誤日志

rewrite_log on|off

是否把重寫過(guò)程記錄在錯(cuò)誤日志中;默認(rèn)為notice級(jí)別;默認(rèn)為off;


grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

error_log /var/log/nginx/error.log notice;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?80;

? ? ? ? server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? ?html;

? ? ? ? ? ? index? index.html index.htm;

? ? ? ? }

? ? ? ? error_page? ?500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? ?html;

? ? ? ? }

? ? }

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? server_name 192.168.60.12;

? ? ? ? root /var/www/html;

? ? ? ? allow 192.168.0.0/16;

? ? ? ? deny all;


? ? ? ? location /admin/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? auth_basic "admin_area";

? ? ? ? ? ? auth_basic_user_file /etc/nginx/.htpasswd;

? ? ? ? }


? ? ? ? location /download/ {

? ? ? ? ? ? root /var/www/html/;

? ? ? ? ? ? autoindex on;

? ? ? ? ? ? autoindex_exact_size off;

? ? ? ? ? ? autoindex_localtime on;

? ? ? ? ? ? charset utf-8;

? ? ? ? ? ? add_after_body /autoindex.html;

? ? ? ? ? ? rewrite ^/download/(.*\.(jpg|gif|jpeg|png))$ /images/$1 break;

? ? ? ? ? ? rewrite_log on;

? ? ? ? }


? ? }

}



8、return code:?

用于結(jié)束rewrite規(guī)則,并且為客戶返回狀態(tài)碼;可以使用的狀態(tài)碼有204, 400, 402-406, 500-504等;



9、配置HTTPS,并將HTTP重定向到HTTPS


用openssl實(shí)現(xiàn)私有CA:

生成密鑰對(duì)兒:

cd /etc/pki/CA

(umask 077; openssl genrsa -out private/cakey.pem 2048)

生成自簽證書:

openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3655

創(chuàng)建需要的文件:

touch index.txt serial crlnumber

echo "00" > serial


用openssl實(shí)現(xiàn)證書申請(qǐng):

在主機(jī)上生成密鑰,保存至應(yīng)用此證書的服務(wù)的配置文件目錄下, 例如:

mkdir /etc/httpd/ssl

cd /etc/httpd/ssl

(umask 077; openssl genrsa -out httpd.key 1024)


生成證書簽署請(qǐng)求:

openssl req -new -key httpd.key -out httpd.csr?

將請(qǐng)求文件發(fā)往CA;


CA簽署證書:

簽署:

openssl ca -in /path/to/somefile.csr -out /path/to/somefile.crt -days DAYS



配置HTTPS

grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

error_log /var/log/nginx/error.log notice;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? ?443 ssl;

? ? ? ? server_name? www.;

? ? ? ? ssl_certificate? ? ? /etc/httpd/ssl/httpd.crt;

? ? ? ? ssl_certificate_key? /etc/httpd/ssl/httpd.key;

? ? ? ? ssl_session_cache? ? shared:SSL:1m;

? ? ? ? ssl_session_timeout? 5m;

? ? ? ? ssl_ciphers? HIGH:!aNULL:!MD5;

? ? ? ? ssl_prefer_server_ciphers? on;

? ? }

}


將HTTP重定向到HTTPS

grep -v ^$ /etc/nginx/nginx.conf | grep -v .*#

worker_processes? 1;

error_log /var/log/nginx/error.log notice;

events {

? ? worker_connections? 1024;

}

http {

? ? include? ? ? ?mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? 80;

? ? ? ? server_name www.;

? ? ? ? root /var/www/html;

? ? ? ? rewrite ^(.*) https://$server_name$1 permanent;

? ? }

? ? server {

? ? ? ? listen? ? ? ?443 ssl;

? ? ? ? server_name? www.;

? ? ? ? ssl_certificate? ? ? /etc/httpd/ssl/httpd.crt;

? ? ? ? ssl_certificate_key? /etc/httpd/ssl/httpd.key;

? ? ? ? ssl_session_cache? ? shared:SSL:1m;

? ? ? ? ssl_session_timeout? 5m;

? ? ? ? ssl_ciphers? HIGH:!aNULL:!MD5;

? ? ? ? ssl_prefer_server_ciphers? on;

? ? ? ? root /var/www/html;

? ? }

}


來(lái)源:http://www./content-3-113601.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多