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版本

  1. lg@lg-PC:~$ nginx -v
  2. nginx version: nginx/1.3.10

nginx配置文件默认目录结构

  1. /etc/nginx/
  2. ├── conf.d
  3.    ├── default.conf
  4.    ├── example_ssl.conf
  5. ├── fastcgi.conf
  6. ├── fastcgi_params
  7. ├── koi-utf
  8. ├── koi-win
  9. ├── mime.types
  10. ├── nginx.conf
  11. ├── scgi_params
  12. ├── uwsgi_params
  13. └── win-utf
  14.  
  15. 1 directory, 11 files

增加 Nginx 虚拟主机

配置 Virtual host 步骤如下:

1.检查/etc/nginx/nginx.conf配置文件,确保文件中有:include /etc/nginx/conf.d/*.conf;   例如:

  1. user lg;
  2. worker_processes 2;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. debug_connection 127.0.0.1;
  8. debug_connection 192.168.1.0/24;
  9. }
  10. http {
  11. include /etc/nginx/mime.types;
  12. default_type application/octet-stream;
  13. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. '$status $body_bytes_sent "$http_referer" '
  15. '"$http_user_agent" "$http_x_forwarded_for"';
  16. access_log /var/log/nginx/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. keepalive_timeout 65;
  20. client_max_body_size 13m;
  21. #gzip on;
  22. include /etc/nginx/conf.d/*.conf;
  23. }

2.关键步骤,在目录/etc/nginx/conf.d/下面新建文件site1.conf,site2.conf,文件名任意写,自己看明白就OK,后缀名需要与步骤1配置的一致,这里为.conf。site1代表我们的第一个站点,site2代表我们的第二个站点,下面我们看看两个文件都需要写点什么:

site1.conf:

  1. server {
  2. listen 80;
  3. server_name ~^\d+\.\d+\.\d+\.\d+$;
  4. #charset koi8-r;
  5. error_page 404 /404.html;
  6. # redirect server error pages to the static page /50x.html
  7. #
  8. error_page 500 503 504 /50x.html;
  9. error_log /var/log/nginx/debug.log debug;
  10. index index.html index.htm;
  11. root /home/lg/www/;
  12.  
  13. location /svn {
  14. root /home/lg/www/;
  15. index index.html;
  16. }
  17.  
  18. location = /favicon.ico {
  19. try_files $uri $uri/favicon.ico /home/lg/www/favicon.ico =404;
  20. }
  21.  
  22. location /share {
  23. root /home/lg/Downloads;
  24. }
  25.  
  26. # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
  27. location ~ /\. {
  28. deny all;
  29. }
  30.  
  31. location ^~ /packages {
  32. root /home/lg/Downloads/1software;
  33. autoindex on;
  34. autoindex_exact_size on;
  35. autoindex_localtime on;
  36. allow all;
  37. }
  38.  
  39. location ^~ /Music {
  40. root /home/lg/;
  41. autoindex on;
  42. autoindex_exact_size on;
  43. autoindex_localtime on;
  44. allow all;
  45. }
  46.  
  47. location ^~ /Videos {
  48. root /home/lg/;
  49. autoindex on;
  50. autoindex_exact_size on;
  51. autoindex_localtime on;
  52. allow all;
  53. }
  54.  
  55. location ^~ /html5 {
  56. root /home/lg/workspace/nodejs/;
  57. index index.html index.htm;
  58. }
  59.  
  60. location ^~ /NginxStatus {
  61. stub_status on;
  62. access_log on;
  63. #auth_basic 'NginxStatus';
  64. #auth_basic_user_file conf.d/htpasswd
  65. }
  66.  
  67. location = /50x.html {
  68. root /usr/share/nginx/html;
  69. }
  70.  
  71. location = /404.html {
  72. root /usr/share/nginx/html;
  73. }
  74. }

site2.conf:

  1. server {
  2. listen 80;
  3. server_name ~^openlg.net$;
  4. root /home/lg/workspace/phpworkspace/wp;
  5. index index.php index.html index.htm;
  6. location = /favicon.ico {
  7. try_files /home/lg/www/favicon.ico =404;
  8. #log_not_found off;
  9. #access_log off;
  10. }
  11.  
  12. # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
  13. location ~ /\. {
  14. deny all;
  15. }
  16.  
  17. location ~* /(?:uploads|files)/.*\.php$ {
  18. deny all;
  19. }
  20.  
  21. location / {
  22. try_files $uri $uri/ /index.php?$args;
  23. }
  24.  
  25. 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)$ {
  26. access_log off;
  27. log_not_found off;
  28. expires max;
  29. }
  30.  
  31. location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  32. expires 24h;
  33. log_not_found off;
  34. }
  35.  
  36. error_page 404 /404.html;
  37.  
  38. error_page 500 502 503 504 /50x.html;
  39. location = /50x.html {
  40. root /usr/share/nginx/html;
  41. }
  42.  
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. location ~ \.php$ {
  46. try_files $uri =404;
  47. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  48. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  49. include fastcgi_params;
  50. fastcgi_pass 127.0.0.1:9000;
  51. }
  52. }

3.测试配置文件,没问题就加载新配置文件

  1. lg@lg-PC:~$ sudo nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful
  4. lg@lg-PC:~$ sudo kill -HUP `cat /var/run/nginx.pid`
  5. lg@lg-PC:~$

4.打开文件/etc/hosts,添加

  1. 127.0.0.1 openlg.net

5.打开浏览器分别请求下面的地址进行测试,如果相应内容不一样,那么咱们就大功告成了。

  1. http://127.0.0.1
  2.  
  3. 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的虚拟主机)的更多相关文章

  1. Apache服务器在80端口配置多域名虚拟主机的方法

    我们在配置一台服务器的时候,如果只运行一个站点,往往过于浪费资源.Nginx和Apache都可以通过配置虚拟主机实现多站点.配置虚拟主机的方式主要有两种,一种是多个不同端口对应的多个虚拟主机站点,一种 ...

  2. Nginx配置基于多域名、端口、IP的虚拟主机

    原文:https://www.cnblogs.com/ssgeek/p/9220922.html ------------------------------- Nginx配置基于多域名.端口.IP的 ...

  3. nginx配置多个虚拟主机(mac)

    1 . 安装  通过homebrew安装nginx,默认安装在:/usr/local/Cellar/nginx/版本号.配置文件在路径:/usr/local/etc/nginx ,默认配置文件ngin ...

  4. nginx配置多个虚拟主机vhost

    在nginx下配置虚拟主机vhost非常方便.主要在nginx的配置文件nginx.conf中添加一个server即可 比如我想配置两个虚拟主机,通过域名linux.com和linux2.com访问, ...

  5. Nginx入门讲解——初步认识了解nginx.conf配置文件以及配置多个虚拟主机

    本文引自网络进攻学习之用https://blog.csdn.net/weixin_38111957/article/details/81080539 一. 引言上节文章讲述了如何用信号控制Nginx服 ...

  6. nginx配置基于域名、端口、IP的虚拟主机

    1.基于域名的虚拟主机: 绝大多数企业对外提供服务的网站使用的都是基于域名的主机,通过不同的域名区分不同的虚拟主机. 首先我们进入安装nginxd的目录下:/application/nginx-1.6 ...

  7. [Nginx]Nginx的基本配置与优化1(完整配置示例与虚拟主机配置)

    ---------------------------------------------------------------------------------------- 完整配置示例: [ n ...

  8. 基于Apache在本地配置多个虚拟主机站点

    简单的说,打开httpd.conf 在最后加入如下内容: <VirtualHost 127.0.0.2:80>    DocumentRoot d:/AppServ/www2    Ser ...

  9. Apache基于域名、端口、IP的虚拟主机配置(Centos 6.5)

    虚拟主机:部署多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,不同的ip,需要虚拟主机功能.一句话,一个http服务要配置多个站点,就需要虚拟主机. 虚拟主机分类:基于域名.基于端口 ...

随机推荐

  1. C. Tourist's Notes

    题目链接 题意:n天内登山,相邻两次登山的高度差的绝对值小于等于1,也就是说每次高度变化只能是:0,1,-1.我们已经知道n天中部分天数人所在的山的高度,求最大的登山高度. 输入: n m  n 是天 ...

  2. RESTful登录设计(基于Spring及Redis的Token鉴权)

    转载自:http://www.scienjus.com/restful-token-authorization/ http://m.blog.csdn.net/article/details?id=4 ...

  3. kill -9 和kill

    kill pid 在kill进程的同时,会将包删掉该进程所在webapps目录下的文件夹,如iexpense文件夹 kill -9 pid 会强制删掉进程,但是不是删掉该进程所在webapps目录下的 ...

  4. python_pycharm介绍1

    1. 常用设置 修改编程风格 File-Setting中,Editor下Colors&Fonts修改即可调整风格. 修改字体大小 pycharm默认字体太小,需调整些,Settings--&g ...

  5. 学了C语言,如何写个程序计算出每个月的第一个星期一对应的日期

    在前面,我们分别利用泰勒公式和C标准库中的mktime()函数推算了某个特定日期所对应的星期几,刚做完这些,就又遇到了一个与日期相关的新任务: 老板把每个月例会的时间定在了每个月的第一个星期一,他让我 ...

  6. [iOS]如何删除工程里面用cocoapods导入的第三方库

    如何在工程中卸载用cocoapods导入的第三方呢? 1. 打开工程所在文件夹 2. 打开Podfile文件 3. 删除不要的第三方导入命令 4. 然后在回到终端,然后进入到工程目录下,然后更新第三方 ...

  7. Android 软键盘弹出时把布局顶上去,控件乱套解决方法

    解决办法:方法一:在你的activity中的oncreate中setContentView之前写上这个代码getWindow().setSoftInputMode(WindowManager.Layo ...

  8. angular todo

    <!DOCTYPE HTML> <html lang="en-US" ng-app> <head> <meta charset=" ...

  9. Difference between Pragma and Cache-control headers?

    Pragma is the HTTP/1.0 implementation and cache-control is the HTTP/1.1 implementation of the same c ...

  10. sql2000无法打开1433端口及解决方法

    1.如果你是win2003,那么一定要安装sql的补丁sp3a以上版本SP 检查你的SQL有没有打补丁,没有的话要打上补丁,检查的方法是在查询分析器中运行:select @@version如果出来的版 ...