nginx 基于域名的虚拟主机配置

假如nginx配置文件目录是:/usr/local/nginx/conf

1.如果该目录下没有文件fcgi.conf,则添加该文件内容如下:

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

2.在nginx配置文件nginx.conf的http段,添加

include vhost/*.conf;

3. 假如服务器ip是192.168.1.17.  我想使用的域名是my17.com.

则,在目录./vhost下面添加配置文件my17.com.conf,内容如下:

server
        {
                listen       80;
                server_name my17.com www.my17.com;
                index index.php index.html index.htm default.html default.htm default.php;
                root /home/mywww;
                access_log off;
}

在服务器上创建目录 /home/mywww, 并且在该目录下创建默认首页文件index.html


4. 检查nginx配置文件是否正确,如果正确,平滑重启nginx

nginx -t
nginx -s reload

5. 配置自己电脑的host文件,使域名my17.com可以解析到服务器

编辑C:\WINDOWS\system32\drivers\etc\hosts

添加如下行:

192.168.1.17 my17.com www.my17.com

最后,在浏览器输入my17.com.  可以访问了。

你可能感兴趣的:(nginx 基于域名的虚拟主机配置)