Nginx 配置示例
2016-12-16 00:00
316 查看
tcp 代理
HTTP 透明代理
两个虚拟主机(纯静态-html 支持)
注:此方法不需要为每一个域名建立一个 vhost.conf 配置文件。
负载均衡
一个简单的负载均衡的示例,把www.domain.com均衡到本机不同的端口,也可以改为均衡到不同的地址上。
nginx 防盗链
前面的root可以不要如果你在server{}中有设置可以不需要设定。
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{}中有设置可以不需要设定。
相关文章推荐
- Nginx升级
- How to enable Nginx FancyIndex on Ubuntu
- nginx常用命令
- 为最佳性能调优 Nginx
- lvs、haproxy、nginx 负载均衡的比较分析
- 2016-12-15 Nginx 之初学者(一)
- FastDFS学习笔记
- Nginx+Varnish 实现动静分离,为服务器分流,降低服务器负载
- 【夯实Nginx基础】Nginx工作原理和优化、漏洞
- (总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解
- NGINX+keepalived
- Nginx的安装
- [原创]# 玩转nginx系列
- nginx设置请求body大小
- Nginx安装
- nginx 一般网站部署常用参数设置
- nginx缓存引发的问题
- nginx缓存引发的问题
- Nginx配置location总结及rewrite规则写法
- Nginx与Apache比较