且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

nginx.conf配置文件各项详解(建议收藏查阅)(下)

更新时间:2022-05-13 00:44:42

##其他举例


server


{

listen 80;


server_name .**.com .**.cn;


location ~* .(mp3|exe)$ {

proxy_pass http://img_relay$request_uri;


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


}


location / {

if ($http_user_agent ~* “xnp”) {

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;


}


proxy_pass http://img_relay$request_uri;


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


#error_page 404 http://i1.***img.com/help/noimg.gif;


error_page 404 502 = @fetch;


}

location @fetch {

access_log /data/logs/baijiaqi.log log404;


rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;


}


}


server


{

listen 80;


server_name *.***img.com;


location ~* .(mp3|exe)$ {

proxy_pass http://img_relay$request_uri;


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


}


location / {

if ($http_user_agent ~* “xnp”) {

rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;


}


proxy_pass http://img_relay$request_uri;


proxy_set_header Host $host;


proxy_set_header X-Real-IP $remote_addr;


proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


#error_page 404 http://i1.***img.com/help/noimg.gif;


error_page 404 = @fetch;


}


#access_log off;


location @fetch {

access_log /data/logs/baijiaqi.log log404;


rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;


}


}

server


{

listen 8080;


server_name ngx-ha.***img.com;


location / {

stub_status on;


access_log off;


}


}


server {

listen 80;


server_name imgsrc1.***.net;


root html;


}


server {

listen 80;


server_name ***.com w.***.com;


access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.)$ http://www.**.com/ ;


}


}


server {

listen 80;


server_name .com w..com;


access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.)$ http://www.******.com/;


}

}


server {

listen 80;


server_name ******.com;


access_log /usr/local/nginx/logs/access_log main;

location / {

rewrite ^(.)$ http://www.*****.com/;


}


}


location /NginxStatus {

stub_status on;

access_log on;

auth_basic “NginxStatus”;

auth_basic_user_file conf/htpasswd;

}


#设定查看Nginx状态的地址


location ~ /.ht {

deny all;

}

#禁止访问.htxxx文件


}


注释:变量


Ngx_http_core_module模块支持内置变量,他们的名字和apache的内置变量是一致的。


首先是说明客户请求title中的行,例如h t t p u s e r a g e n t , http_user_agent,http

u

ser

a

gent,http_cookie等等。


此外还有其它的一些变量


$args此变量与请求行中的参数相等


$content_length等于请求行的“Content_Length”的值。


$content_type等同与请求头部的”Content_Type”的值


$document_root等同于当前请求的root指令指定的值


d o c u m e n t u r i 与 document_uri与document

u

ri与uri一样

$host与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样


$limit_rate允许限制的连接速率


$request_method等同于request的method,通常是“GET”或“POST”


$remote_addr客户端ip


$remote_port客户端port


$remote_user等同于用户名,由ngx_http_auth_basic_module认证


$request_filename当前请求的文件的路径名,由root或alias和URI request组合而成


$request_body_file


$request_uri含有参数的完整的初始URI


q u e r y s t r i n g 与 query_string与query

s

tring与args一样


$sheeme http模式(http,https)尽在要求是评估例如


Rewrite ^(.+)$ s h e m e : / / e x a m p l e . c o m sheme://example.comsheme://example.com; Redirect;

$server_protocol等同于request的协议,使用“HTTP/或“HTTP/


$server_addr request到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。


$server_name请求到达的服务器名


$server_port请求到达的服务器的端口号


$uri等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index