上一篇我们写到了怎么为Nginx配置ssl证书,使其支持https访问,但是我们服务器在使用过程中呢,不可能一台服务器只配置一个网站。今天就来说一下怎么为Nginx绑定多个域名。
配置方法很简单,只需要新建Nginx配置文件中的server段就可以了,需要注意的就是{}括号的使用,大部分的报错都是因为{}括号没有用对。
普通http站点的配置
如果只是普通的http站点,不需要其他配置的话,那么是最简单的。只需要在nginx配置文件http段下server段下再新建一个server段并写入如下配置文件。
1 2 3 4 5 6 7 8 9 10 11 12 13
| server { listen 80; server_name yourname; #写入你的域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root /home/www/; #写入你的站点根目录路径 index index.html index.htm; } }
|
一定要注意 } 不要搞错了,一段配置文件完一般都是两个 }
需要使用php的站点配置
假如是用来搭建olaindex等需要php支持的站点,那么需要加入php的代码段。和上面http段的基本一样,需要服务器已经安装了php。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server { listen 80; server_name yourname; # 你的域名 location / { root /home/www/; # 你的站点根目录 index index.php index.html; } location ~ ^(.+\.php)(.*)$ { root /home/www/; # 你的站点根目录 fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; if (!-e $document_root$fastcgi_script_name) { return 404; } fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
|
https站点的配置
还是和上面一样的,只不过这次需要新建两个server段,一个是http的一个是https下的。
如果是第一次配置 ssl,建议点这里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| # http段 这一行不用写 server { listen 80; server_name yourname; # 这两行都一样 yourname都是填写你的域名 return 301 https://yourname$request_uri;
#charset koi8-r;
#access_log logs/host.access.log main;
location / { root /home/; # 你的站点根目录 index index.html index.htm; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| # https段 这一行不用写 server { listen 443 ssl; server_name yourname; # 你的域名 #ssl on; ssl_certificate /; # 你的ssl证书pem文件路径 ssl_certificate_key /; # 你的ssl证书key文件路径
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
location / { root /home/; # 你的站点根目录 index index.html index.htm; } }
|
配置完之后,保存退出vim编辑器,输入nginx -t测试一下配置文件是否可用,出现successful表示可用。之后重启一下nginx就可以了。
如果报错的话,仔细检测一下配置文件,然后再试。
如果提示nginx命令不存在的话,说明你还没有为nginx配置系统变量,点这里为nginx配置系统变量。