本地多项目通过Nginx来配置

在开发环境中,经常会遇到多个项目同时运行的时候,那么现在通过nginx来实现不同域名访问不同的项目。
一般的配置分为两种,一种是在nginx.conf中直接配置(不利于扩展,耦合性高),另外一种是一个域名对应一个配置文件。
现选择第二种配置方法,例如一个服务器上面需要配置域名为www.nginxtest1.com,www.nginxtest2.com操作步骤为:
1、在C:\Windows\System32\drivers\etc目录中新建vhosts文件夹。
2、在vhosts文件夹下面分别新建www.nginxtest1.com.conf、www.nginxtest2.com.conf两个文件
3、以www.nginxtest1.com.conf配置为例,如下:

server
{
listen 80;
server_name ceshi.banma.com;   ------------》这里写入你自己的域名(www.nginxtest1.com)
index index.html index.htm index.php default.html default.htm default.php;
root /var/data/www/banma;

location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*)  $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}

location ~ [^/]\.php(/|$)
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
try_files $fastcgi_script_name =404; 
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}


location ~ /\.(ht|svn|bzr|git|hg|cvs) {
deny all;
}
}

4、在nginx安装路径中nginx.conf 文件中,包含刚创建的两个文件
include /etc/vhosts/*
6、重启nginx服务器:service nginx reload
7、在本地的hosts文件里面加上 127.0.0.1 www.nginxtest1.com,127.0.0.1 www.nginxtest2.com

nginx详解:Nginx的代理功能和负载均衡功能是最常被用到的。
1、只接受get或者post请求方法
proxy_method get;
2、include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat ’ remoteaddr remote_user [ timelocal] request status body_bytes_sent httpreferer http_user_agent $http_x_forwarded_for’; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本。
#proxy_method get; #支持客户端的请求方法。post/get;
proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
proxy_ignore_headers “Expires” “Set-Cookie”; #Nginx服务器不处理设置的http相应投中的头域,这里空格隔开可以设置多个。
proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符。
proxy_headers_hash_bucket_size 128; #nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。
proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
#proxy_ssl_session_reuse on; 默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off。
负载均衡详解:
1、热备:如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务。服务器处理请求的顺序:AAAAAA突然A挂啦,BBBBBBBBBBBBBB…..
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:333 backup ;}
2、轮询:nginx默认的就是轮询所有权重为1,服务器请求顺序ABABABABAB…..
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333;}
3、加权轮询:根据配置的权重的大小而分发给不同服务器不同数量的请求,如果不设置,则默认为1,下面服务器请求顺序为:ABBABBABBABBABB……
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:333 weight=2 ;}
4、ip_hash:nginx会让相同的客户端ip请求Ian共同的服务器。
5、其他一些配置
down,表示当前的server暂时不参与负载均衡。
backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值