nginx配置虚拟主机

nginx 是一个小巧高效的 web 服务器,由俄罗斯程序员 Igor Sysoev 开发,nginx 虽然体积小,但功能一点也不弱,能和其他的 web 服务器一样支持 virtual hosting,即一个IP对应多个域名以支持多站点访问,就像一个IP对应一个站点一样,所以是”虚拟”的。

这里以配置2个站点(2个域名)为例,n 个站点可以相应增加调整,假设:

IP地址: 202.55.1.100
域名1 ysy1.com 放在 /www/ysy1
域名2 ysy22.com 放在 /www/ysy2

配置 nginx virtual hosting 的基本思路和步骤如下:

把2个站点 ysy1.com, ysy2.com 放到 nginx 可以访问的目录 /www/
给每个站点分别创建一个 nginx 配置文件 ysy1.com.conf,ysy2.com.conf, 并把配置文件放到 /etc/nginx/vhosts/
然后在 /etc/nginx.conf 里面加一句 include 把步骤2创建的配置文件全部包含进来(用 * 号)
重启 nginx

 

具体步骤:

1、在 /usr/local/etc/nginx 下创建 vhosts 目录

1 mkdir /etc/nginx/vhosts

 

 

2、在 /usr/local/etc/nginx/vhosts/ 里创建一个名字为 ysy1.com.conf 的文件,把以下内容拷进去

 1 server {
 2         listen  80;#端口要和nginx端口一样,我的由8080默认端口改到80
 3         server_name ysy1.com www.ysy1.com;
 4 
 5         #access_log  /www/access_ysy1.log;
 6 
 7         location / {
 8             root   /www/ysy1.com;#放置访问文件的目录,
 9             index  index.php index.html index.htm;#访问文件的检索顺序
10         }
11 
12         # error_page   500 502 503 504  /50x.html;
13         location = /50x.html {
14             root   html;  #随便,这里我没有设置
15         }
16 
17        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
18         location ~ \.php$ {
19             fastcgi_pass   127.0.0.1:9000;
20             fastcgi_index  index.php;
21             fastcgi_param  SCRIPT_FILENAME  /www/ysy1.com/$fastcgi_script_name;#fastcgi需要配置
22             include        fastcgi_params;
23         }
24         location ~ /\.ht {
25             deny  all;
26         }
27 }

 

 

 

3、在 /usr/local/etc/nginx/vhosts/ 里创建一个名字为 ysy2.com.conf 的文件,把上边内容拷进去,将所有ysy1改称ysy2

 

4、打开 /usr/local/etc/nginix.conf 文件,在相应位置加入 include 把以上2个文件包含进来

  1 user  www www;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  localhost;
 37 
 38         #charset koi8-r;
 39 
 40         #access_log  logs/host.access.log  main;
 41 
 42         location / {
 43             root   html;
 44             index  index.html index.htm;
 45         }
 46 
 47         #error_page  404              /404.html;
 48 
 49         # redirect server error pages to the static page /50x.html
 50         #
 51         error_page   500 502 503 504  /50x.html;
 52         location = /50x.html {
 53             root   html;
 54         }
 55 
 56         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 57         #
 58         #location ~ \.php$ {
 59         #    proxy_pass   http://127.0.0.1;
 60         #}
 61 
 62         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 63         #
 64         location ~ \.php$ {
 65             root           html;
 66             fastcgi_pass   127.0.0.1:9000;
 67             fastcgi_index  index.php;
 68             fastcgi_param  SCRIPT_FILENAME  /usr/local/Cellar/nginx/1.6.2/html$fastcgi_script_name;
 69             include        /usr/local/etc/nginx/fastcgi_params;
 70         }
 71 
 72         # deny access to .htaccess files, if Apache's document root
 73         # concurs with nginx's one
 74         #
 75         #location ~ /\.ht {
 76         #    deny  all;
 77         #}
 78     }
 79 
 80 
 81     # another virtual host using mix of IP-, name-, and port-based configuration
 82     #
 83     #server {
 84     #    listen       8000;
 85     #    listen       somename:8080;
 86     #    server_name  somename  alias  another.alias;
 87 
 88     #    location / {
 89     #        root   html;
 90     #        index  index.html index.htm;
 91     #    }
 92     #}
 93 
 94 
 95     # HTTPS server
 96     #
 97     #server {
 98     #    listen       443 ssl;
 99     #    server_name  localhost;
100 
101     #    ssl_certificate      cert.pem;
102     #    ssl_certificate_key  cert.key;
103 
104     #    ssl_session_cache    shared:SSL:1m;
105     #    ssl_session_timeout  5m;
106 
107     #    ssl_ciphers  HIGH:!aNULL:!MD5;
108     #    ssl_prefer_server_ciphers  on;
109 
110     #    location / {
111     #        root   html;
112     #        index  index.html index.htm;
113     #    }
114     #}
115 
116 
117     #include all vhosts
118     include /usr/local/etc/nginx/vhosts/*.conf;
119 }

 

 

5、建立nginx对应的访问目录及文件

1 sudo mkdir /www/ysy1.com/
2 vim /www/ysy1.com/index.php

 

index.php内部调用phpinfo(); 

6,修改hosts配置,重启 Nginx

1 sudo vim /etc/hosts
 1 cat /etc/hosts
 2 ##
 3 # Host Database
 4 #
 5 # localhost is used to configure the loopback interface
 6 # when the system is booting.  Do not change this entry.
 7 ##
 8 127.0.0.1    localhost
 9 255.255.255.255    broadcasthost
10 ::1             localhost 
11 fe80::1%lo0    localhost
12 127.0.0.1       ysy1.com
13 127.0.0.1       ysy2.com
14 127.0.0.1       www.ysy1.com #访问什么配什么,与conf配置文件无关
15 127.0.0.1       www.ysy2.com

 

posted @ 2015-01-07 22:41  Sungyou_Yu  阅读(227)  评论(0编辑  收藏  举报