仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 380|回复: 8
打印 上一主题 下一主题

[其他Linux] 给大家带来nginx 多站点设置办法汇合

[复制链接]
再现理想 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 11:46:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
对于开发环境的选择尽量要轻量级和高度可定制,航空母舰级别的工具往往会让你迷惑不解;
那末我们入手下手吧:
1、为我们的站点创立设置文件
  我是这么做的,在nginx的设置文件conf目次下创立一个专门寄存VirtualHost的目次,定名为vhosts_conf,能够把假造目次的设置全体放在这里。在内里创立名为vhosts_modoupi_websuitA.conf的设置文件并翻开,我们在这里做设置,往内里写:
.代码以下:

server{
listen80;               #监听的端标语
server_namewebsuitA.com;        #域名
#access_loglogs/host.access.logmain;
location/{
rootX:/wnmp/www/websuitA;    #站点的路径
indexdefault.phpindex.phpindex.htmlindex.htm;
#站点的rewrite在这里写
rewrite^/(w+).html$/$1.php;
rewrite^/(w+)/(w+)$/$1/$2.php;
}
#毛病页的设置
error_page404/error.html;
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
location~.php${
rootX:/wnmp/www/websuitA;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
includefastcgi_params;
}
location~/.ht{
denyall;
}
}


如许就做好了,站点A的设置,一样的办法,做websuitB的设置,这里我定名为vhosts_modoupi_websuitB.conf,间接上代码
.代码以下:

server{
    listen80;               #监听的端标语
    server_namewebsuitB.com;        #域名
    #access_loglogs/host.access.logmain;
    location/{
      rootX:/wnmp/www/websuitB;    #站点的路径
      indexdefault.phpindex.phpindex.htmlindex.htm;
#站点的rewrite在这里写
       rewrite^/(w+).html$/$1.php;
       rewrite^/(w+)/(w+)$/$1/$2.php;
     }
 #毛病页的设置
     error_page404/error.html;
     error_page500502503504/50x.html;
     location=/50x.html{
       roothtml;
     }
    #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
    location~.php${
        rootX:/wnmp/www/websuitB;
        fastcgi_pass127.0.0.1:9000;
        fastcgi_indexindex.php;
        fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
        includefastcgi_params;
     }
     location~/.ht{
        denyall;
     }
}


如许,两个站点的设置就OK了。
2、在nginx的主设置文件里,包括这两个站点的设置文件。
  我们翻开conf目次下的nginx.conf文件,很简单做,只需在http{...}段输出以下代码:
.代码以下:

#包括一切的假造主机的设置文件
includeX:/wnmp/nginx/conf/vhosts_conf/*.conf;


如许,nginx的多站点设置就做好了,怎样翻开扫瞄器测试一下吧~

第二种办法:
当我们有了一个VPS主机今后,为了不华侈VPS的壮大资本(比拟共享主机1000多个站点挤在一台呆板上),常常有想让VPS做点甚么的设法,银子不克不及白花啊:)。安排多个网站大概博客是个不错的设法,但是怎样设置web服务器才干在一个VPS上安排多个网站/博客呢?怎样经由过程一个IP会见多个站点/域名呢?这就是年夜多半web服务器撑持的virtualhosting功效。这里将形貌怎样一步一步怎样用nginx设置virtualhosting。
nginx是一个玲珑高效的web服务器,由俄罗斯程序员IgorSysoev开辟,nginx固然体积小,但功效一点也不弱,能和其他的web服务器一样撑持virtualhosting,即一个IP对应多个域名以撑持多站点会见,就像一个IP对应一个站点一样,以是是”假造”的。你想在一个IP上面放几个站点就放几,只需硬盘够年夜就行。
这里以设置2个站点(2个域名)为例,n个站点能够响应增添调剂,假定:
IP地点:202.55.1.100
域名1example1.com放在/www/example1
域名2example2.com放在/www/example2
设置nginxvirtualhosting的基础思绪和步骤以下:
把2个站点example1.com,example2.com放到nginx能够会见的目次/www/
给每一个站点分离创立一个nginx设置文件example1.com.conf,example2.com.conf,并把设置文件放到/etc/nginx/vhosts/
然后在/etc/nginx.conf内里加一句include把步骤2创立的设置文件全体包括出去(用*号)
重启nginx
详细历程
上面是详细的设置历程:
1、在/etc/nginx下创立vhosts目次
1
mkdir/etc/nginx/vhosts
2、在/etc/nginx/vhosts/里创立一个名字为example1.com.conf的文件,把以下内容拷出来
.代码以下:

server{
listen80;
server_nameexample1.comwww.example1.com;
access_log/www/access_example1.logmain;
location/{
root/www/example1.com;
indexindex.phpindex.htmlindex.htm;
}
error_page500502503504/50x.html;
location=/50x.html{
root/usr/share/nginx/html;
}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
location~.php${
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/www/example1.com/$fastcgi_script_name;
includefastcgi_params;
}
location~/.ht{
denyall;
}
}


3、在/etc/nginx/vhosts/里创立一个名字为example2.com.conf的文件,把以下内容拷出来
.代码以下:

server{
listen80;
server_nameexample2.comwww.example2.com;
access_log/www/access_example1.logmain;
location/{
root/www/example2.com;
indexindex.phpindex.htmlindex.htm;
}
error_page500502503504/50x.html;
location=/50x.html{
root/usr/share/nginx/html;
}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
location~.php${
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/www/example2.com/$fastcgi_script_name;
includefastcgi_params;
}
location~/.ht{
denyall;
}
}


4、翻开/etc/nginix.conf文件,在响应地位到场include把以上2个文件包括出去
.代码以下:

usernginx;
worker_processes1;
#mainservererrorlog
error_log/var/log/nginx/error.log;
pid/var/run/nginx.pid;
events{
worker_connections1024;
}
#mainserverconfig
http{
includemime.types;
default_typeapplication/octet-stream;
log_formatmain$remote_addr-$remote_user[$time_local]$request
"$status"$body_bytes_sent"$http_referer"
"$http_user_agent""$http_x_forwarded_for";
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
keepalive_timeout65;
gzipon;
server{
listen80;
server_name_;
access_log/var/log/nginx/access.logmain;
server_name_in_redirectoff;
location/{
root/usr/share/nginx/html;
indexindex.html;
}
}
#包括一切的假造主机的设置文件
include/usr/local/etc/nginx/vhosts/*;
}


5、重启Nginx
第三种办法:
一个服务器上必要跑多个网站,假如仅仅把域名剖析到server的IP是不可的,会见分歧域名翻开的都是nginx默许的网站。要想分离对应,必要在nginx里设置vhost。

我是用lnmp一键安装包(http://www.lnmp.org/)安装的nginx+mysql+php情况,关于其他本人编译的nginx估量设置文件和安装目次会有所分歧,本人酌情修正哦,呵呵
编纂/usr/local/nginx/conf/nginx.conf,往失落server的参数。
.代码以下:

server
{
listen80;
server_namewww.wifizoo.net;
indexindex.htmlindex.htmindex.php;
root/tmp/wwwroot;本文来自
location~.*.(php|php5)?$
{
fastcgi_passunix:/tmp/php-cgi.sock;
fastcgi_indexindex.php;
includefcgi.conf;
}copyright
location/status{
stub_statuson;
access_logoff;
}
copyright
location~.*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires30d;
}

location~.*.(js|CSS)?$
{
expires12h;
}

log_formataccess$remote_addr-$remote_user[$time_local]"$request"
$status$body_bytes_sent"$http_referer"
"$http_user_agent"$http_x_forwarded_for;
access_log/home/wwwroot/logs/access.logaccess;
}


然后到场vhost界说:copyright
include/usr/local/nginx/vhost/*.conf;
}
再在/usr/local/nginx/创建vhost文件夹,内里创立各域名的对应设置文件。
这个复杂,就把之前的server设置内容复制到创立的对应conf文件里就OK了。
.代码以下:

server
{
listen80;
server_namewww.ckuyun.com;
server_namejb51.net;
indexindex.htmlindex.htmindex.php;
root/tmp/wwwroot/meituge;

location~.*.(php|php5)?$
{
fastcgi_passunix:/tmp/php-cgi.sock;
fastcgi_indexindex.php;
includefcgi.conf;
}copyright
location/status{
stub_statuson;
access_logoff;
}
copyright

location~.*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires30d;
}
copyright

location~.*.(js|css)?$
{
expires12h;
}
#log_formataccess$remote_addr-$remote_user[$time_local]"$request"
#$status$body_bytes_sent"$http_referer"
#"$http_user_agent"$http_x_forwarded_for;
#access_log/home/wwwroot/logs/access.logaccess;
}


这里要注重,假如你用的是一级域名,那末必要在server设置里指定不加www前缀的域名,不然会见jb51.net会被界说到默许站点而非www.ckuyun.com
server_namewww.ckuyun.com;
server_namejb51.net;
RedHatCentOS等等.学习linux不是逛自由市场,选定版本就要静下心来学习.不要今天换版本明天要升级.这样对你没有好处。
深爱那片海 该用户已被删除
沙发
发表于 2015-1-18 08:15:32 | 只看该作者
Linux是参照Unix思想设计的,理解掌握Linux必须按照Unix思维来进行。思想性的转变比暂时性的技术提高更有用,因为他能帮助你加快学习速度。
再见西城 该用户已被删除
板凳
发表于 2015-1-26 12:50:46 | 只看该作者
得到到草率的回答或者根本得不到任何Linux答案。越表现出在寻求帮助前为解决问题付出的努力,你越能得到实质性的帮助。
柔情似水 该用户已被删除
地板
发表于 2015-2-4 18:49:41 | 只看该作者
对于英语不是很好的读者红旗 Linux、中标Linux这些中文版本比较适合。现在一些Linux网站有一些Linux版本的免费下载,这里要说的是并不适合Linux初学者。
山那边是海 该用户已被删除
5#
发表于 2015-2-10 06:00:04 | 只看该作者
对我们学习操作系统有很大的帮助,加深我们对OS的理解。?
灵魂腐蚀 该用户已被删除
6#
发表于 2015-2-28 23:30:40 | 只看该作者
另外Linux上也有很多的应用软件,安装运行了这些软件后,你就可以在Linux上编辑文档、图?片,玩游戏、上网、播放多媒体文件等。
活着的死人 该用户已被删除
7#
发表于 2015-3-10 10:54:05 | 只看该作者
把这个问题放在其他Linux社区请求帮助也是一种选择。如果Linux得不到答案,请不要以为我们觉得无法帮助你。有时只是看到你问题的人不知道答案罢了。这时换一个社区是不错的选择。
若天明 该用户已被删除
8#
发表于 2015-3-17 07:08:29 | 只看该作者
可以说自己收获很大,基本上完成了老师布置的任务,对于拔高的题目没有去做,因为我了解我的水平,没有时间和精力去做。?
小女巫 该用户已被删除
9#
发表于 2015-3-24 01:51:46 | 只看该作者
虽然大家都比较喜欢漂亮的mm,但是在学 linux 的过程中,还是要多和“男人”接触一下:P 遇到问题的时候,出来看说和上网查之外,就是要多用 linux 下的 man 命令找找帮助。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-27 20:50

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表