标签:
一台nginx服务器同一IP被注册多个不同域名,访问不同域名到该服务器后请求不同项目 本台nginx服务器的IP地址为 192.168.155.129 假设服务器有两个项目websuit_a,websuit_b,客户端访问websuit_a.com时请求websuit_a项目,访问websuit_b.com时请求websuit_b项目 首先在项目根目录下新建项目文件夹websuit_a和websuit_b存放两个虚拟站点的程序 mkdir -m777 -p /usr/local/nginx/html/websuit_a /usr/local/nginx/html/websuit_b 在websuit_a项目下新建index.php文件,并输入以下内容 <?php echo "this is websuit_a";?> vim /usr/local/nginx/html/websuit_a/index.php 在websuit_b项目下新建index.php文件,并输入以下内容 <?php echo "this is websuit_b";?> vim /usr/local/nginx/html/websuit_b/index.php 在nginx配置文件夹内新建vhosts_conf文件夹保存不同虚拟站点的配置文件 mkdir -m777 -p /usr/local/nginx/conf/vhosts_conf 在里面创建websuit_a.com站点的配置文件,命名为websuit_a.conf编辑填写以下内容保存 server { listen 80; #监听的端口号 server_name websuit_a.com; #客户端访问进来的域名 #access_log logs/host.access.log main; location / { root html/websuit_a; #站点的项目路径也可填成绝对路径/usr/local/nginx/html/websuit_a index index.html index.htm index.php; #站点的rewrite在这里写,URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程 rewrite ^/(\w+)\.html$ /$1.php; rewrite ^/(\w+)/(\w+)$ /$1/$2.php; } #错误页的配置 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { root html/websuit_a; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/websuit_a$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } } vim /usr/local/nginx/conf/vhosts_conf/websuit_a.conf 再创建websuit_b.com站点的配置文件,命名为websuit_b.conf编辑填写以下内容保存 server { listen 80; #监听的端口号 server_name websuit_b.com; #客户端访问进来的域名 #access_log logs/host.access.log main; location / { root html/websuit_b; #站点的项目路径也可填成绝对路径/usr/local/nginx/html/websuit_b index index.html index.htm index.php; #站点的rewrite在这里写 rewrite ^/(\w+)\.html$ /$1.php; rewrite ^/(\w+)/(\w+)$ /$1/$2.php; } #错误页的配置 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { root html/websuit_b; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/websuit_b$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } } vim /usr/local/nginx/conf/vhosts_conf/websuit_b.conf 关闭nginx服务 service nginx stop 编辑nginx配置文件在http {}块内的最后一行添加如下内容 include /usr/local/nginx/conf/vhosts_conf/*.conf; vim /usr/local/nginx/conf/nginx.conf 启动nginx service nginx start 由于不是外网环境,只能在本地访问,所以修改客户端hosts文件,添加以下两项 192.168.155.129 websuit_a.com 192.168.155.129 websuit_b.com 分别在客户端浏览器上输入 websuit_a.com,websuit_b.com 分别显示 this is websuit_a,this is websuit_b
linux Nginx VirtualHost虚拟主机多站点设置
标签:
原文地址:http://www.cnblogs.com/dreamhome/p/5100656.html