您的位置:首页 > 运维架构 > Nginx

Nginx 配置示例

2016-12-16 00:00 316 查看
tcp 代理

stream {
upstream backend {
server 127.0.0.1:10020 max_fails=3 fail_timeout=10s;
server 127.0.0.1:10022 max_fails=3 fail_timeout=10s;
}

server {
listen 2080;
proxy_connect_timeout 20s;
proxy_timeout 5m;
proxy_pass backend;
}
}

HTTP 透明代理

proxy_buffering off;

server {
listen  8088;
location / {
proxy_pass http://$http_host$request_uri; }
}

两个虚拟主机(纯静态-html 支持)

server {

listen       80;  #could also be 1.2.3.4:80 也可以是1.2.3.4:80的形式

server_name  star.yourdomain.com *.yourdomain.com www.*.yourdomain.com;

root /PATH/TO/WEBROOT/$host;
error_page  404              http://yourdomain.com/errors/404.html; access_log  logs/star.yourdomain.com.access.log;

location / {
root   /PATH/TO/WEBROOT/$host/;
index  index.php;
}

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log        off;
expires           30d;
}

location ~ .php$ {
# By all means use a different server for the fcgi processes if you need to
# 如果需要,你可以为不同的FCGI进程设置不同的服务信息
fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
fastcgi_param  QUERY_STRING     $query_string;
fastcgi_param  REQUEST_METHOD   $request_method;
fastcgi_param  CONTENT_TYPE     $content_type;
fastcgi_param  CONTENT_LENGTH   $content_length;
fastcgi_intercept_errors on;
}

location ~ /\.ht {
deny  all;
}
}

注:此方法不需要为每一个域名建立一个 vhost.conf 配置文件。

负载均衡

http {
upstream myproject {
server 127.0.0.1:8000 weight=3;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject; }
}
}

一个简单的负载均衡的示例,把www.domain.com均衡到本机不同的端口,也可以改为均衡到不同的地址上。

nginx 防盗链

location ~* \.(gif|jpg|png|swf|flv)$ {
root html
valid_referers none blocked *.nginxcn.com;
if ($invalid_referer) {
rewrite ^/ [www.nginx.cn](http://www.nginx.cn/)
#return 404;
}
}

前面的root可以不要如果你在server{}中有设置可以不需要设定。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: