这里以配置2个站点对应2个不一样域名为例
操做环境:ubuntu 16.04 64位 nginx/1.10.3
假设:
IP地址: 111.111.111.111
域名1 example1.com 放在 /www/example1
域名2 example2.com 放在 /www/example2
配置 的基本思路和步骤以下:
1.把2个站点 example1.com, example2.com 放到 nginx 能够访问的目录 /www/
2.给每一个站点分别建立一个 nginx 配置文件 example1.com.conf,example2.com.conf,
并把配置文件放到 /etc/nginx/sites-available/下,
3.把/etc/nginx/sites-available/的example1.com.conf,example2.com.conf两个文件,软链接到/etc/nginx/sites-enabled/目录下
4.确认在 /etc/nginx/nginx.conf 里面有一句php
include /etc/nginx/sites-enabled/*;
这样就会自动引用sites-enabled目录下的全部配置文件。通常会有,若没有这句,就手动加一下。
ps:至于为嘛要搞个连接,而不直接把文件放到sites-enabled下,默认的default配置文件就是如此,你看sites-enabled下的default文件的属性:html
lrwxrwxrwx 1 root root 34 Dec 23 22:09 default -> /etc/nginx/sites-available/default
就是个软链接,源文件在sites-available下。因此这个地方,咱们也遵循这个原则。
5.去掉/etc/nginx/nginx.conf中的server_names_hash_bucket_size 前的注释,并修改数值为32的整数,例如128
server_names_hash_bucket_size 128;
6.最后重启 nginxnginx
说明:
在example1.com.conf的配置文件中,server_name对应访问的域名,配置成功后,在域名管理页面,添加对应的域名记录,指向服务器的IP。
若运行后报错:Nginx: could not build the server_names_hash ,检查步骤5 的设置。
example1.com.conf example2.com.conf配置文件示例:ubuntu
server { listen 80; server_name example1.com www. example1.com; access_log /www/access_ example1.log main; location / { root /www/example1.com; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # 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 /www/example1.com/$fastcgi_script_name; include fastcgi_params; } location ~ /.ht { deny all; } }