欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx:vhost

程序员文章站 2024-01-09 13:11:40
...
nginx配置多个vhost
1.进入配置安装目录 cd /usr/local/etc/nginx/conf/
2.没有vhost文件夹 mkdir vhsot文件
3.添加多个conf文件 如:test.com.conf test1.com.conf
4.test.com.conf
  server {
    listen          80;       
    server_name     test.com;       
    index           index.html index.htm index.php;       
    root            /Users/suncg/www/test.com;        
    log_format      test.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer' '$http_user_agent $http_x_forwarded_for';       
    access_log      /var/log/test.com.log test.com;
}

test1.com.conf
server {
    listen          80;       
    server_name     test1.com;       
    index           index.html index.htm index.php;       
    root            /Users/suncg/www/test1.com;        
    log_format      test1.com '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer' '$http_user_agent $http_x_forwarded_for';       
    access_log      /var/log/test1.com.log test1.com;
}
5.在/Users/suncg/www/  mkdir test.com 和 mkdir test1.com 以及在响应文件夹下创建 index.html 内容为1111 和 2222
6.在/usr/local/conf/nginx.conf http{}部分添加include       vhost/*.conf;
如:
http
{
      #include       conf/mime.types;
      include       mime.types;
      default_type  application/octet-stream;
      include       vhost/*.conf;

      keepalive_timeout 120;

      tcp_nodelay on;

      upstream  www.samson.com  {
              server   127.0.0.1:8080;
              # server   192.168.1.3:80;
              # server   192.168.1.4:80;
              # server   192.168.1.5:80;
      }

      server
      {
              listen  80;
              server_name  www.samson.com;

              location / {
                       proxy_pass        http://www.samson.com;
                       proxy_set_header   Host             $host;
                       proxy_set_header   X-Real-IP        $remote_addr;
                       proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
              }

              log_format  www_samson_com  '$remote_addr - $remote_user [$time_local] $request '
                                '"$status" $body_bytes_sent "$http_referer" '
                                '"$http_user_agent" "$http_x_forwarded_for"';
              access_log  logs/www.log  www_samson_com;
      }

}

配置host
127.0.0.1  test.com
127.0.0.1  test1.com
访问test.com 显示1111 访问test1.com显示2222
相关标签: nginx vhost