Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)
Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 消耗资源小, 无论是静态服务器还是网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高,目前很多大型网站都在使用Nginx做为 Web 服务器,例如:人人网。另外淘宝研发大军针对大访问量网站的需求,对Nginx做了专门的定制,添加了很多高级功能和特性(Tengine),Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。
本文将讲解如何在Ubuntu Linux上使用 Nginx Web服务器来实现一台电脑一个端口(80)搭建多个网站。
绝大多数的 Nginx 运行在 Linux 机器上, 虽然有 Windows 移植版,但在Windows下的测试发现Nginx发挥不是很好. 所以本文将以 Linux 为例讲解, 而 Mac OS 或其他 Unix like 机器上的操作应该是一样的.
Nginx版本
lg@lg-PC:~$ nginx -v
nginx version: nginx/1.3.10
nginx配置文件默认目录结构
/etc/nginx/
├── conf.d
│ ├── default.conf
│ ├── example_ssl.conf
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf 1 directory, 11 files
增加 Nginx 虚拟主机
配置 Virtual host 步骤如下:
1.检查/etc/nginx/nginx.conf配置文件,确保文件中有:include /etc/nginx/conf.d/*.conf; 例如:
user lg;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
debug_connection 127.0.0.1;
debug_connection 192.168.1.0/24;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 13m;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
2.关键步骤,在目录/etc/nginx/conf.d/下面新建文件site1.conf,site2.conf,文件名任意写,自己看明白就OK,后缀名需要与步骤1配置的一致,这里为.conf。site1代表我们的第一个站点,site2代表我们的第二个站点,下面我们看看两个文件都需要写点什么:
site1.conf:
server {
listen 80;
server_name ~^\d+\.\d+\.\d+\.\d+$;
#charset koi8-r;
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 503 504 /50x.html;
error_log /var/log/nginx/debug.log debug;
index index.html index.htm;
root /home/lg/www/;
location /svn {
root /home/lg/www/;
index index.html;
}
location = /favicon.ico {
try_files $uri $uri/favicon.ico /home/lg/www/favicon.ico =404;
}
location /share {
root /home/lg/Downloads;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
location ~ /\. {
deny all;
}
location ^~ /packages {
root /home/lg/Downloads/1software;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
allow all;
}
location ^~ /Music {
root /home/lg/;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
allow all;
}
location ^~ /Videos {
root /home/lg/;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
allow all;
}
location ^~ /html5 {
root /home/lg/workspace/nodejs/;
index index.html index.htm;
}
location ^~ /NginxStatus {
stub_status on;
access_log on;
#auth_basic 'NginxStatus';
#auth_basic_user_file conf.d/htpasswd
}
location = /50x.html {
root /usr/share/nginx/html;
}
location = /404.html {
root /usr/share/nginx/html;
}
}
site2.conf:
server {
listen 80;
server_name ~^openlg.net$;
root /home/lg/workspace/phpworkspace/wp;
index index.php index.html index.htm;
location = /favicon.ico {
try_files /home/lg/www/favicon.ico =404;
#log_not_found off;
#access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
log_not_found off;
expires max;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
}
3.测试配置文件,没问题就加载新配置文件
lg@lg-PC:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
lg@lg-PC:~$ sudo kill -HUP `cat /var/run/nginx.pid`
lg@lg-PC:~$
4.打开文件/etc/hosts,添加
127.0.0.1 openlg.net
5.打开浏览器分别请求下面的地址进行测试,如果相应内容不一样,那么咱们就大功告成了。
http://127.0.0.1 http://openlg.net
到这里,大家也许已经明白怎么回事了,我再罗嗦两句,我这里的site1.conf相当与一个静态文件服务器,site2.conf是一个php的网站,大家可以看到配置文件中加粗显示的两行:listen和server_name分别代表监听端口和虚拟主机名称。80端口没得说,重点解释下server_name,server_name用的是正则表达式,~^\d+\.\d+\.\d+\.\d+$匹配所有的ip地址,~^openlg.net$匹配openlg.net,这里也可以直接写成server_name 127.0.0.1;或者server_name openlg.net;
当我们请求http://127.0.0.1/时,nginx会使用两个server_name配置的正则表达式分别去测试请求地址中的127.0.0.1来决定使用那个虚拟主机,这里当然就是使用site1.conf中配置的server了。当我们请求http://openlg.net/时,nginx就会选择使用site2.conf中配置的server了。
Nginx实现多个站点使用一个端口(配置Nginx的虚拟主机)的更多相关文章
- Apache服务器在80端口配置多域名虚拟主机的方法
我们在配置一台服务器的时候,如果只运行一个站点,往往过于浪费资源.Nginx和Apache都可以通过配置虚拟主机实现多站点.配置虚拟主机的方式主要有两种,一种是多个不同端口对应的多个虚拟主机站点,一种 ...
- Nginx配置基于多域名、端口、IP的虚拟主机
原文:https://www.cnblogs.com/ssgeek/p/9220922.html ------------------------------- Nginx配置基于多域名.端口.IP的 ...
- nginx配置多个虚拟主机(mac)
1 . 安装 通过homebrew安装nginx,默认安装在:/usr/local/Cellar/nginx/版本号.配置文件在路径:/usr/local/etc/nginx ,默认配置文件ngin ...
- nginx配置多个虚拟主机vhost
在nginx下配置虚拟主机vhost非常方便.主要在nginx的配置文件nginx.conf中添加一个server即可 比如我想配置两个虚拟主机,通过域名linux.com和linux2.com访问, ...
- Nginx入门讲解——初步认识了解nginx.conf配置文件以及配置多个虚拟主机
本文引自网络进攻学习之用https://blog.csdn.net/weixin_38111957/article/details/81080539 一. 引言上节文章讲述了如何用信号控制Nginx服 ...
- nginx配置基于域名、端口、IP的虚拟主机
1.基于域名的虚拟主机: 绝大多数企业对外提供服务的网站使用的都是基于域名的主机,通过不同的域名区分不同的虚拟主机. 首先我们进入安装nginxd的目录下:/application/nginx-1.6 ...
- [Nginx]Nginx的基本配置与优化1(完整配置示例与虚拟主机配置)
---------------------------------------------------------------------------------------- 完整配置示例: [ n ...
- 基于Apache在本地配置多个虚拟主机站点
简单的说,打开httpd.conf 在最后加入如下内容: <VirtualHost 127.0.0.2:80> DocumentRoot d:/AppServ/www2 Ser ...
- Apache基于域名、端口、IP的虚拟主机配置(Centos 6.5)
虚拟主机:部署多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,不同的ip,需要虚拟主机功能.一句话,一个http服务要配置多个站点,就需要虚拟主机. 虚拟主机分类:基于域名.基于端口 ...
随机推荐
- iOS开发--完整项目
完整项目 Phonetic Swift 写的一个 iOS 版的 Phonetic Contacts,功能很多,其中昵称功能非常实用,已在 GitHub 开源并上架 App Store v2ex – v ...
- Android:Intel Atom x86模拟器的安装与使用
1.下载硬件加速执行管理器haxm-windows_r05.zip,找到系统对应版本,并安装 2.下载安卓x86系统映象sysimg_x86-19_r01.zip,不区分系统环境,解压得到x86文件夹 ...
- CentOS查看系统信息命令和方法
收集整理的一些linux查看系统信息的命令和方法: 一.linux查看服务器系统信息的方法: 1.查看主机名/内核版本/CPU构架: # uname -n -r -p -o localhost.loc ...
- java Cache框架
Cache框架乱炖 各类开源的缓存解决方案 JBossCache/TreeCacheJBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的改善性能.缓存数据被自动复制,让 ...
- 【转载】【JQuery学习】jQuery插件开发
JQuery做得最好的就是他的闭包和扩展性.超级简单的扩展方法,让更多的人可以轻松的开发定制自己的jQuery插件.下面的东西是转载过来当做学习材料的.虽然貌似有点古老,但是jQuery的变更一直都不 ...
- mysql中的连接
SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据. join可以分为内连接和外连接,外连接分为左连接.右连接和全连接 现有两个表 员工表和部门表 员工表 部门表 1.内连接( ...
- Linux守护进程详解(init.d和xinetd) [转]
一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台 的守护进程来执行的 ...
- Uubuntu 14.04 LTS反编译apk
使用apktool反编译apk 1.安装apktool apktool是Google提供的APK编译工具,能够反编译及回编译apk,需要Java环境的支持(在此不再赘述Java的安装与配置,详见< ...
- 利用ExtJS导出Excel
Ext.ns("Msp.Component"); //config = { // fileName : "净值及头寸核对", // exportDate : & ...
- make clean、make mrproer、make distclean
make clean.make mrproer 以及make distclean的区别 解压内核源码包后, 到内核源代码目录树的顶层目录, 执行# make helpCleaning targets: ...