免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
70、Nginx配置實戰(zhàn)

正常運行的必備配置:

1、user username [groupname];

指定運行worker進程的用戶和組

2、pid /path/to/pidfile_name;

指定nginx的pid文件

3、worker_rlimit_nofile #;

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

4、worker_rlimit_sigpending #;

設定每個用戶能夠發(fā)往worker進程的信號的數(shù)量;


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

1、worker_processes #;

worker進程的個數(shù);通常其數(shù)值應該為CPU的物理核心數(shù)減1;

2、worker_cpu_affinity cpumask ...;

指定worker只運行在哪顆CPU上,運行在哪顆CPU上,對應位為1。

worker_processes 6;?

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000;?

3、ssl_engine device;

在存在ssl硬件加速器的服務器上,指定所使用的ssl硬件加速設備;

4、timer_resolution t;

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

5、worker_priority nice;

-20,19之間的值;


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

1、accept_mutex [on|off]

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

2、lock_file /path/to/lock_file;?

lock文件

3、accept_mutex_delay #ms;

accept鎖模式中,一個worker進程為取得accept鎖的等待時長;如果某worker進程在某次試圖取得鎖時失敗了,至少要等待#ms才能再一次請求鎖;

4、multi_accept on|off;

是否允許一次性地響應多個用戶請求;默認為Off;?

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

定義使用的事件模型,建議讓nginx自動選擇;

6、worker_connections #;

每個worker能夠并發(fā)響應最大請求數(shù);


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

1、daemon on|off;

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

2、master_process on|off

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

3、error_log /path/to/error_log level;

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


nginx的http web功能:

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

http {

directive value;

...


server {


}

server {


}

...

}


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

1、server {}

定義一個虛擬主機;nginx支持使用基于主機名或IP的虛擬主機;

2、listen?

listen address[:port];

listen port?


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

rcvbuf=SIZE: 接收緩沖大?。?/p>

sndbuf=SIZE: 發(fā)送緩沖大??;

ssl: https server;

3、server_name [...];

server_name可以跟多個主機名,名稱中可以使用通配符和正則表達式(通常以~開頭);當nginx收到一個請求時,會取出其首部的server的值,而后跟眾server_name進行比較;比較方式:

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

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

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

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

4、server_name_hash_bucket_size 32|64|128;

為了實現(xiàn)快速主機查找,nginx使用hash表來保存主機名;

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

? ?location @name { ... }

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


? ?=:精確匹配;

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

? ?~*:正則表達式模式匹配,匹配時忽略字符大小寫

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

? ?

匹配優(yōu)先級:

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


文件路徑定義:

1、root path

設置web資源路徑;用于指定請求的根文檔目錄;

location / {

root /www/htdocs;

}


location ^~ /images/ {

root /web;

}

2、alias path

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

location / {

root /www/htdocs;

}


location ^~ /images/ {

alias /web;

}

3、index file ...;

定義默認頁面,可參跟多個值;

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

當對于某個請求返回錯誤時,如果匹配上了error_page指令中設定的code,則重定向到新的URI中。

錯誤頁面重定向;

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

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


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

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

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

? ? ? ? }

? ? ? ??

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

1、keepalive_timeout time;

保持連接的超時時長;默認為75秒;

2、keepalive_requests n;

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

3、keepalive_disable [msie6 | safari | none ]

對指定的瀏覽器禁止使用長連接;

4、tcp_nodelay on|off

對keepalive連接是否使用TCP_NODELAY選項;

5、client_header_timeout time;?

讀取http請求首部的超時時長;

6、client_body_timeout time;

讀取http請求包體的超時時長;

7、send_timeout time;

發(fā)送響應的超時時長;


對客戶端請求的限制:

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

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

limit_except GET {

allow 172.16.0.0/16;

deny all;?

}

2、client_max_body_size SIZE;

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

3、limit_rate speed;

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

4、limit_rate_after time;

nginx向客戶發(fā)送響應報文時,如果時長超出了此處指定的時長,則后續(xù)的發(fā)送過程開始限速;


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

1、sendfile on|off

是否啟用sendfile功能;

2、aio on|off

是否啟用aio功能;

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

是否打開文件緩存功能;

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

inactive: 某緩存條目在指定時長時沒有被訪問過時,將自動被刪除;默認為60s;?


緩存的信息包括:

文件句柄、文件大小和上次修改時間;

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

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

4、open_file_cache_errors on|off

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

5、open_file_cache_valid time;

多長時間檢查一次緩存中的條目是否超出非活動時長,默認為60s;?

6、open_file_cache_min_use #;

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


對客戶端請求的特殊處理:

1、ignore_invalid_headers on|off

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

2、log_not_found on|off

是否將文件找不到的信息也記錄進錯誤日志中;

3、resolver address;

指定nginx使用的dns服務器地址;

4、resover_timeout time;

指定DNS解析超時時長,默認為30s;?

5、server_tokens on|off;

是否在錯誤頁面中顯示nginx的版本號;


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

1、client_body_in_file_only on|clean|off

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

2、client_body_in_single_buffer on|off;

HTTP的包體是否存儲在內(nèi)存buffer當中;默認為off;

3、cleint_body_buffer_size size;

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

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

HTTP包體存放的臨時目錄;

5、client_header_buffer_size size;

正常情況下接收用戶請求的http報文header部分時分配的buffer大小;默認為1k;

6、large_client_header_buffers number size;?

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

7、connection_pool_size size;

nginx對于每個建立成功的tcp連接都會預先分配一個內(nèi)存池,此處即用于設定此內(nèi)存池的初始大?。荒J為256;

8、request_pool_size size;

nginx在處理每個http請求時會預先分配一個內(nèi)存池,此處即用于設定此內(nèi)存池的初始大小;默認為4k;?


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

$uri: 當前請求的uri,不帶參數(shù);

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

$host: http請求報文中host首部;如果請求中沒有host首部,則以處理此請求的虛擬主機的主機名代替;

$hostname: nginx服務運行在的主機的主機名;

$remote_addr: 客戶端IP

$remote_port: 客戶端Port

$remote_user: 使用用戶認證時客戶端用戶輸入的用戶名;

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

$request_method: 請求方法

$server_addr: 服務器地址

$server_name: 服務器名稱

$server_port: 服務器端口

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

$scheme: 在請求中使用scheme, 如https://www.nginxtest.com/中的https;

$http_HEADER: 匹配請求報文中指定的HEADER,$http_host匹配請求報文中的host首部

$sent_http_HEADER: 匹配響應報文中指定的HEADER,例如$http_content_type匹配響應報文中的content-type首部;

$document_root:當前請求映射到的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://nginx.org/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、配置虛擬主機

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.a.com;

? ? ? ? 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.a.com;

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

? ? ? ? allow 192.168.53.0/24;

? ? ? ? deny all;

? ? }

}


3、配置用戶認證(訪問www.a.com/admin會彈出認證界面)

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.a.com;

? ? ? ? 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、通過authoindex配置下載站點

編譯的時候需要加 --with-http_addition_module

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

https://raw.githubusercontent.com/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.a.com;

? ? ? ? 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.b.org/403.html?

}?


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


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.a.com;

? ? ? ? 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.a.com *.a.com;

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

? ? ? ? ? ? ? ? #rewrite ^/ http://www.a.com/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.a.com</h1>

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


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

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

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



6、配置URL rewrite

rewrite regex replacement [flag];


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

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

redirect: 返回302臨時重定向;

permanent: 返回301永久重定向;


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

注意:一般將rewrite寫在location中時都使用break標志,或者將rewrite寫在if上下文中;


當訪問到download目錄下的jpg、gif、jpeg、png時自動跳轉(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.a.com;

? ? ? ? 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、配置重寫記錄到錯誤日志

rewrite_log on|off

是否把重寫過程記錄在錯誤日志中;默認為notice級別;默認為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.a.com;

? ? ? ? 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實現(xiàn)私有CA:

生成密鑰對兒:

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實現(xiàn)證書申請:

在主機上生成密鑰,保存至應用此證書的服務的配置文件目錄下, 例如:

mkdir /etc/httpd/ssl

cd /etc/httpd/ssl

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


生成證書簽署請求:

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

將請求文件發(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.a.com;

? ? ? ? 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.a.com;

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

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

? ? }

? ? server {

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

? ? ? ? server_name? www.a.com;

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

? ? }

}


來源:http://www.icode9.com/content-3-113601.html
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
nginx控制指定ip訪問網(wǎng)站
nginx反向代理和負載均衡的配置一樣?
Nginx.conf 配置詳解
nginx作為web服務以及nginx.conf詳解
Nginx的泛域名綁定終于搞好了 - 火魔網(wǎng)
一個命令,讓你的網(wǎng)站支持https
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服