OSChina 的 Nginx 配置,包括三个文件 nginx.conf, proxy.conf, gzip.conf。
1.nginx.conf
user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
limit_zone one $binary_remote_addr 10m;
include gzip.conf;
server {
listen 80;
server_name localhost;
location / {
#rewrite (.*) http://www.oschina.net$1 permanent;
deny all;
}
location ~ ^/NginxStatus {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 219.136.242.37;
allow 192.168.2.0/24;
deny all;
}
}
server {
listen 80;
server_name www.oschina.net m.oschina.net my.oschina.net;
log_format oschina_log
'$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/oschina.log;
location ~ ^/NginxStatus {
stub_status on;
access_log off;
auth_basic "valid-user";
auth_basic_user_file /opt/ngx/conf/oschina_pw;
}
location ~ ^/mrtg {
auth_basic "valid-user";
auth_basic_user_file /opt/ngx/conf/oschina_pw;
access_log off;
root /opt/mrtg;
}
location ~ ^/awstats/ {
root /data/oschina/webapp;
access_log off;
error_log off;
auth_basic "valid-user";
auth_basic_user_file /opt/ngx/conf/oschina_pw;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
location ~ ^/uploads/ {
root /data/oschina/webapp;
expires 24h;
valid_referers none blocked *.google.com *.qq.com *.oschina.net *.csdn.net *.gzv8.com;
if ($invalid_referer){
rewrite ^/ http://www.oschina.net/img/logo.gif;
}
}
location ~ .(htm|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf)$ {
root /data/oschina/webapp;
access_log off;
expires 24h;
}
location / {
proxy_pass http://localhost:9081;
include proxy.conf;
}
error_page 502 503 /502.html;
error_page 404 /404.html;
error_page 403 /403.html;
}
}
2.proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
gzip_min_length 1000;
gzip_types text/plain text/css application/x-javascript;
图片服务器使用Nginx 的proxy_store把主服务器的静态内容缓存到本地。
一次访问结束后,之后的访问将直接在本地硬盘上读写,分担了流量负载。
配置代码:
include listen.conf;
server_name ucenter.gznow.org;
location ~ .php$ {
limit_conn one 20;
limit_rate 50k;
proxy_pass http://s1;
include proxy.conf;
}
location / {
expires max;
root /data/nginx_cache/ucenter;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /data/nginx_cache/ucenter;
include proxy.conf;
if ( !-e $request_filename) {
proxy_pass http://s1;
}
}
}
#######################
## 附 proxy.conf 内容 ##
#######################
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding 'gzip';
client_max_body_size 100m;
client_body_buffer_size 256k;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 512k;
proxy_buffers 8 512k;
proxy_busy_buffers_size 512k; proxy_temp_file_write_size 512k;
您可能感兴趣的文章:
Nginx负载均衡与反向代理的例子(图文)
Nginx Proxy 代理配置图片缓存的实例参考
nginx正向代理配置简单一例
nginx反向代理配置简单示例
学习Nginx反向代理实现简单负载均衡(图文)
nginx缓存html静态文件 解析php及反向代理IIS的配置
nginx中配置proxy正向代理
Nginx实现简单的反向代理
nginx创建反向代理和虚拟主机的例子
nginx的反向代理配置与优化
nginx反向代理与varnish缓存配置
nginx反向代理与负载均衡
Nginx 反向代理的小例子
nginx反向代理与缓存详解
nginx反向代理配置一例
Nginx反向代理Nginx
Nginx反向代理及负载均衡配置
nginx反向代理配置和优化
nginx配置反向代理的简单示例
对于实现访问网站或目录密码认证保护,nginx的HTTP基本认证模块(HTTP Auth Basic)可以实现.
这个模块提供基于用户名与密码的验证来保护你的站点或站点的一部分。
代码:
auth_basic "Restricted";
auth_basic_user_file conf/htpasswd;
}
auth_basic
指令包含一个具有测试用户名和密码的HTTP基本认证,指定的参数将用于认证域。如果将值设置为“off”则忽略下级指令继承的动作。
auth_basic_user_file
指令为验证域指定了密码文件,0.6.7版本以后这里指定的文件是nginx.conf所在目录的相对路径,而不是–prefix指定的路径。
文件格式如下:
user:pass
user2:pass2:comment
user3:pass3
如果没有apache的htpasswd程序,可以使用pl程序生成:
程序地址:http://trac.edgewall.org/export/10890/trunk/contrib/htpasswd.py
使用方法: ./htpasswd.py -b -c pass.txt admin 123456
pass.txt为密码文件,admin是用户名,123456是密码。