本文是對Nginx經常使用配置的整理及記錄。php
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
複製代碼
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/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;
#}
}
複製代碼
http_stub_status_module:nginx客戶端狀態html
location /status {
stub_status;
}
複製代碼
http_sub_module:http內容替換node
location / {
root /usr/share/nginx/html;
index index.html index.htm;
sub_filter '要替換的內容' '被替換後的內容';
sub_filter_once off;
}
複製代碼
鏈接頻率限制:limit_conn_modulenginx
語法web
請求頻率限制:limit_reg_module算法
語法apache
limit_conn_zone $binanry_remote_addr zone=conn_zone:1m;
# 同個ip過來請求,每秒只容許一個請求
limit_req_zone $binanry_remote_addr zone=req_zone:1m rate=1r/s;
http {
server {
location / {
root /usr/share/nginx/html;
limit_conn conn_zone 1;
limit_conn req_zone;
limit_conn req_zone burst=3 nodealy;
index index.html index.htm;
}
}
}
複製代碼
location ~ ^/admin.html {
root /usr/share/nginx/html;
allow all;
deny 222.122.191.3;
# deny 222.122.191.0/24;
index index.html index.htm;
}
}
複製代碼
location ~ ^/admin.html {
root /usr/share/nginx/html;
auth_basic "Auth access error!input your password!";
auth_basic_user_file /etc/nginx/auth_conf;
index index.html index.htm;
}
}
複製代碼
location ~ ^/admin.html {
root /usr/share/nginx/html;
expires 24h;
index index.html index.htm;
}
}
複製代碼
location ~ ^/admin.html {
root /usr/share/nginx/html;
add_header Access-Control-Allow-Origin http://www.baidu.com;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
}
}
複製代碼
location ~ .*\.(jpg|png) {
valid_referers blocked 116.66.111.222 ~/google\./;
if($invalid_referer) {
return 403;
}
root /usr/share/nginx/html;
}
}
複製代碼
# 反向代理
location ~ /test_proxy.html$ {
proxy_pass http://127.0.0.1:8080;
}
複製代碼
location / {
if($http_x_forwarded_for !~* "^116\.62\.103\.228") {
return 403;
}
root /usr/share/nginx/html;
index index.html;
}
# 正向代理
location / {
proxy_pass http://$http_host$request_uri;
}
複製代碼
location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;
proxy_max_temp_file_size 256k;
}
# 能夠把上面的配置參數寫到proxy_params文件中再引用
location / {
proxy_pass http://127.0.0.1:8080;
include proxy_params;
}
複製代碼
http {
upstream oden {
server 116.62.103.222:8001 down;
server 116.62.103.222:8002 backup;
server 116.62.103.222:8003 max_fail=1 fail_timeout=10s;
server 116.62.103.222:8004 weight = 5;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oden;
include proxy_params;
}
}
}
複製代碼
upstream oden {
ip_hash;
server 116.62.103.222:8001;
}
upstream oden {
hash $request_uri;
server 116.62.103.222:8001;
}
複製代碼
http {
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=oden_cache:10m max_size=10g inactive=60m use_tep_path=off;
upstream oden {
server 116.62.103.222:8001;
server 116.62.103.222:8002;
}
server {
listen 80;
server_name localhost;
location / {
proxy_cache oden_cache;
proxy_pass http://oden;
# 在這些狀況下緩存的時間
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
add_header Nginx-Cache "$upstrem_cache_status";
# 發生錯誤時跳到下一臺服務器
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params;
}
}
}
複製代碼