FreeBSD:Nginx虚拟主机配置
时间:2020-01-09 10:38:38 来源:igfitidea点击:
问题描述:如何为虚拟主机配置Nginx Web服务器(在基于相同IP地址名称的虚拟主机下托管多个网站)?
解决方法:Nginx允许您在同一台计算机和同一IP地址上托管多个域名。
有两种完成虚拟主机的基本方法:基于名称和基于IP地址或基于ip。
本教程介绍了基于名称的虚拟主机,即您可以为同一Web服务器IP地址托管多个网站(主机名)。
基于Nginx名称的虚拟主机配置
您需要按以下方式创建目录结构,以使用相同的IP地址托管两个以上的网站:
/websites
:在此目录下托管每个域。
您需要按以下方式创建目录:/websites/examples.com/http Html/php/wordpress/forum.com的论坛文件位于此处。
/websites/examples.com/logs example.com的日志文件在此处。
/websites/examples.com/stats awstats example.com的统计信息文件位于此处。
/usr/local/etc/nginx/vhosts
:在此目录下托管每个域配置。
设置示例
- IP地址:192.54.1.2
- HTTP端口:" 80"
- Domain1:theitroad.com托管在/websites/theitroad.com/http
- Domain2:theitroad.local托管在/websites/theitroad.local/http
创建必要的目录
执行以下命令:
# D=/websites # NROOT=/usr/local/etc/nginx # mkdir $D # mkdir $NROOT/vhosts # mkdir /var/log/nginx/ # chown root:www /var/log/nginx/
创建/更新默认缓存所有ngnix配置文件
打开/usr/local/etc/nginx/nginx.conf文件,执行:
# vi /usr/local/etc/nginx/nginx.conf
如下更新:
user www www; worker_processes 1; # main server error log error_log /var/log/nginx/error.log ; pid /var/run/nginx.pid; events { worker_connections 1024; } # main server config http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; # default server for ip 192.54.1.2 server { listen 192.54.1.2:80 default; server_name _; access_log /var/log/nginx/access.log main; server_name_in_redirect off; location / { index index.html; root /usr/local/www/nginx; } } # virtual hosting include /usr/local/etc/nginx/vhosts/*; }
其中:
user www www;
::设置Nginx服务器的用户名和组名。worker_processes 1;
:: nginx能够对大型系统(例如带有大量内存的SMP系统)使用多个工作进程。error_log/var/log/nginx/error.log;
::默认错误日志文件。- pid/var/run/nginx.pid; ::默认PID文件。
include mime.types;
::设置来自mime.types文件的www通信的多用途Internet邮件扩展(MIME)。default_type application/octet-stream;
::设置消息内容的默认头媒体类型log_format main $remote_addr $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for;
::指令log_format描述了日志条目的格式。sendfile on;
::激活sendfile()的用法。tcp_nopush on;
::此伪指令允许或禁止在FreeBSD上使用套接字选项TCP_NOPUSH或在Linux上使用套接字选项TCP_CORK。仅当使用sendfile时,此选项才可用。keepalive_timeout 65;
::设置保持活动超时。gzip on;
::打开gzip听192.54.1.2:80默认;
::听给定的IP:端口。server_name _;
::分配虚拟服务器的名称。access_log /var/log/nginx/access.log main;
::设置访问文件的路径。index index.html;
::设置默认索引文件。root/usr/local/www/nginx;
::设置默认文档根目录。include/usr/local/etc/nginx/vhosts/*;
::处理所有vhosts配置文件。
在第一个虚拟主机目录中创建theitroad.com
执行以下命令:
# mkdir $D/theitroad.com/{http,logs,stats} # chown -R theitroadftpuser:theitroadftpgroup $D/theitroad.com/
theitroad.com Nginx虚拟主机配置文件
打开/usr/local/etc/nginx/vhosts/theitroad.com.conf文件,执行:
# vi $NROOT/vhosts/theitroad.com.conf
追加配置如下:
server { listen 80; server_name theitroad.com www.theitroad.com; access_log /websites/theitroad.com/logs/access.log main; location / { root /websites/theitroad.com/http; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/www/nginx-dist; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /websites/theitroad.com/http$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
保存并关闭文件。
创建theitroad.local第二虚拟主机目录
执行以下命令:
# mkdir $D/theitroad.local/{http,logs,stats} # chown -R user:group $D/theitroad.local/
theitroad.local Nginx虚拟主机配置文件
# vi $NROOT/vhosts/theitroad.local.conf
追加配置如下:
server { listen 80; server_name theitroad.local www.theitroad.local; access_log /websites/theitroad.local/logs/access.log main; location / { root /websites/theitroad.local/http; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/local/www/nginx-dist; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /websites/theitroad.local/http$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
保存并关闭文件。
重新启动Nginx Web服务器,执行:
# nginx -c /usr/local/etc/nginx/nginx.conf -t # /usr/local/etc/rc.d/nginx restart