FreeBSD如何配置Nginx虚拟主机
时间:2019-11-20 08:54:04 来源:igfitidea点击:
问题
在Nginx中,如何配置多个虚拟主机?
Nginx如何在同一台服务器上托管多个网站?
只有1个IP,Nginx如何创建多个虚拟主机托管网站?
解决方案
在Nginx中,创建虚拟主机有两种方法。
基于名称的虚拟主机。
基于IP地址的虚拟主机。
可以在同一个服务器上托管多个域名网站。
基于名称的Nginx虚拟主机配置
要在同一个IP的服务器上托管多个域名网站,可以使用下面的目录结构:
您需要按以下方式创建目录结构,以使用相同的IP地址托管两个以上的网站:
- /websites:在此目录下创建站点目录。
- /websites/examples.com/www 存放html ,css,js等文件
- /websites/examples.com/logs 存放日志文件
- /websites/examples.com/stats 存放awstats的统计信息文件。
- /usr/local/etc/nginx/vhosts:存放每个域名站点的配置。
Nginx虚拟主机配置
假设我们要创建两个虚拟主机,对应不同的域名。
- 服务器IP地址:192.168.1.100
- HTTP端口:80
- 域名1:theitroadtest.com托管在/websites/theitroadtest.com/www
- 域名2:theitroad.local托管在/websites/theitroad.local/www
创建站点目录
执行以下命令:
# mkdir /websites # mkdir /usr/local/etc/nginx/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.168.1.100 server { listen 192.168.1.100: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;
打开gzipListen 192.168.1.100:80
设置默认的监听端口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配置文件。
为第一个虚拟主机theitroadtest.com创建目录
执行以下命令:
# mkdir /websites/theitroadtest.com/{http,logs,stats} # chown -R theosftpuser:theosftpgroup /websites/theitroadtest.com/
theitroadtest.com Nginx虚拟主机配置文件
打开/usr/local/etc/nginx/vhosts/theitroadtest.com.conf文件:
# vi /usr/local/etc/nginx/vhosts/theitroadtest.com.conf
设置如下:
server { listen 80; server_name theitroadtest.com www.theitroadtest.com; access_log /websites/theitroadtest.com/logs/access.log main; location / { root /websites/theitroadtest.com/www; 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/theitroadtest.com/www$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
为第二个虚拟主机theitroad.local创建目录
执行以下命令:
# mkdir /websites/theitroad.local/{http,logs,stats} # chown -R user:group /websites/theitroad.local/
theitroad.local Nginx虚拟主机配置文件
# vi /usr/local/etc/nginx/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/www; 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/www$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
测试
因为server_name分别是theitroadtest.com和theitroad.local。
我们就不能使用ip访问站点了。
而是通过下面url进行访问(这些域名必须做了dns解析)。
http://theitroadtest.com
http://www.theitroadtest.com
http://theitroad.local
http://www.theitroad.local