Nginx添加虚拟主机

我们要添加虚拟主机一般有两种情况,一种是只有一个IP绑定多个域名,另一种是多个IP绑定多个域名,下面我们分别介绍这两种情况下怎么添加虚拟主机。

只有一个IP绑定多个域名

假设我们添加域名为www.centos.bz,根目录为/home/wwwroot/zhumaohai的虚拟主机。
新建文件www.centos.bz.conf。

  1. vi /usr/local/nginx/conf/vhost/www.centos.bz.conf

粘贴如下内容到此文件。

  1. server
  2. {
  3. listen  80 default;
  4. server_name www.centos.bz;
  5. index index.html index.htm index.php default.html default.htm default.php;
  6. root  /home/wwwroot/zhumaohai;
  7.  
  8. include none.conf;
  9. location ~ .*\.(php|php5)?$
  10. {
  11. fastcgi_pass  unix:/tmp/php-cgi.sock;
  12. fastcgi_index index.php;
  13. include fcgi.conf;
  14. }
  15.  
  16. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  17. {
  18. expires      30d;
  19. }
  20.  
  21. location ~ .*\.(js|css)?$
  22. {
  23. expires      12h;
  24. }
  25.  
  26. log_format  www.centos.bz  '$remote_addr - $remote_user [$time_local] $request '
  27.              '$status $body_bytes_sent $http_referer '
  28.              '$http_user_agent $http_x_forwarded_for';
  29. access_log  /home/wwwlogs/www.centos.bz.log  www.centos.bz;
  30. }

server_name:为绑定的域名
index :默认首页
root :根目录
include :是否开启伪静态。

多个IP绑定多个域名

其实也很简单,只要把上面行:listen 80 default;改成listen 8.8.8.8;(8.8.8.8为你绑定的IP)

标签:Nginx 发布于:2019-10-03 16:33:16