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
时间: 2024-07-29 12:36:21

nginx配置虚拟主机的相关文章

LNMP架构应用实战——Nginx配置虚拟主机

LNMP架构应用实战--Nginx配置虚拟主机        前面介绍了nginx服务的安装与配置文件,今天介绍下它的另一种实用配置--"虚拟主机",每个虚拟主机可以是一个独立的网站,可以具有独立的域名,同一台服务器上的不同的虚拟主机之间是独立的,用户访问不同虚拟主机如同访问不同的服务器一样,因此它不需要为一个单独的WEB站点提供单独一个nginx服务器和一个单独的nginx进程 1.nginx虚拟主机简单介绍 同apache服务一样,它也有三种不同的虚拟主机,基于域名的虚拟主机.基于

nginx配置虚拟主机vhost的方法详解

摘自:http://www.jb51.net/article/107331.htm Nginx vhost配置,可实现基于ip.端口号.servername的虚拟主机,同时可避免直接修改主配置文件.在nginx下配置虚拟主机vhost非常方便.这篇文章主要介绍了nginx配置虚拟主机vhost的方法,需要的朋友可以参考下 前言 所谓虚拟主机,是说通过几个不同的url地址,都能到达nginx环境,只不过针对不同的url,处理的逻辑不同.nginx支持虚拟主机,但是浏览器等客户端不知道,所以虚拟主机

nginx配置虚拟主机之不同端口和不同IP地址

配置nginx虚拟主机不同端口和不同ip地址,和上编nginx基于域名配置虚拟主机博文类似,请先参考. zxl.com域名不同端口,配置文件内容如下: [[email protected] conf.d]# cat zxl.com.conf  server { listen 81; server_name www.zxl.com zxl.com; location / { root /data/zxl; index index.html index.htm; access_log  logs/z

nginx 配置虚拟主机的三种方法

nginx,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机--应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机--应用:公司内部网站,外部网站的管理后台 3.基于ip的虚拟主机,几乎不用. 1.基于域名配置虚拟主机配置: 需要建立/data/www /data/bbs目录,windows本地hosts添加虚拟机ip地址对应的域名解析: 对应域名网站目录下新增index.html文件: nginx.conf配置文件新增如下代码: server 

nginx配置虚拟主机之基于域名

安装nginx请参考,nginx编译安装的博文 1:配置nginx虚拟主机,同一个端口80,多个不同的域名.nginx默认主配置文件内容如下 [[email protected] conf]# cat nginx.conf user  nginx; worker_processes  1; error_log  logs/error.log; pid        logs/nginx.pid; events {     worker_connections  1024; } http {   

window下phpstudy的nginx配置虚拟主机

由于很长时间没有配置Apache,虽然说知道怎么配置nginx,但是还是花费了一些时间这次记下来下次直接用 在其他选项文件菜单中->打开配置文件->选择vhosts-conf nginx的话使用 server { listen 80; server_name 你的虚拟目录名称; root "你要操作的目录路径"; location / { index index.html index.htm index.php; #autoindex on; if ($request_fi

Nginx配置虚拟主机(二)

一. 配置基于域名的虚拟主机 [[email protected] conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf [[email protected] conf]# cat nginx.conf worker_processes  1; events {     worker_connections  1024; } http {     include       mime.types;     default_t

nginx配置虚拟主机-端口号区分

Nginx实现虚拟机 可以实现在同一台服务运行多个网站,而且网站之间互相不干扰.同一个服务器可能有一个ip,网站需要使用80端口.网站的域名不同. 区分不同的网站有三种方式:ip区分.端口区分.域名区分,显然通过IP区分是不太现实的,这里只验证后两种方式 1.配置nginx基于ip地址的虚拟主机 1.1 nginx配置文件中添加一个server节点,这里server节点的域名都是localhost,只是端口号不同 1.2 将 /usr/local/nginx/路径下的html目录复制一份,命名为

Nginx配置——虚拟主机基于IP,域名,端口(实战!)

Nginx虚拟主机 基于域名的虚拟主机 基于IP地址的虚拟主机 基于端口的虚拟主机 一,安装DNS域名解析服务器 1,安装bind服务器 [[email protected] ~]# yum install bind -y 2,修改主配置文件(named.conf) [[email protected] ~]# vim /etc/named.conf options { listen-on port 53 { any; }; ##监听所有 listen-on-v6 port 53 { ::1;

Linux下nginx配置虚拟主机

在弄到新服务器,安装了所有东西后,开始配置新的站点测试了,可是问题却随之而来呀,主要是站点设置成功,但是并不支持php.nginx配置站点其实就是基于一ip多站点.那么在配置目录中新建一个配置文件,名字起得和站点名字相同,然后写入: server { listen 80; server_name www.piyaoyan.com piyaoyan.com; access_log /var/log/nginx/piyaoyan.com/access.log main; root /home/akc