# Nginx虚拟主机 [TOC] ## 基于域名的虚拟主机 使用域名`www.domain.com`为例! ~~~ server { listen 80; server_name www.domain.com; error_page 404 /404.html; location / { root html/www; index index.html; access_log logs/www.domain.com_access.log commonlog; error_log logs/www.domain.com_error.log; } location ~ .+\.php.*$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } ~~~ ## 基于端口的虚拟主机 以端口8080为例,域名依然使用`www.domain.com` ~~~ server { listen 8080; server_name www.domain.com; error_page 404 /404.html; location / { root html/port; index index.html; access_log logs/www.domain.com_access.log commonlog; error_log logs/www.domain.com_error.log; } location ~ .+\.php.*$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } ~~~ ## 基于IP的虚拟主机 以`192.168.0.200`为默认Ip配置虚拟主机 ~~~ server { listen 80; server_name 192.168.0.200; error_page 404 /404.html; location / { root html/ipvhosts; index index.html; access_log logs/192.168.0.200_access.log commonlog; error_log logs/192.168.0.200_error.log; } location ~ .+\.php.*$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } } ~~~