网站首页 > 技术文章 正文
nginx服务器如何配置多虚拟站点,操作如下:
1、配置文件目录:除了nginx服务器根目录下的nginx.conf文件外,其它多站点一般配置在目录"conf/vhost"下,演示如下:
目录图:
站点配置文件以“.conf”为后缀,名称一般按域名来起就可以了,多站点创建不同的.conf文件。
2、配置站点,代码如下:
server
{
#listen 80;
listen 443;//端口
server_name 你的域名;
index index.html index.php admincp.php;
root /mnt2/qqck;
#ssl证书开始
ssl on;
ssl_certificate /usr/local/nginx/conf/cert/138.com.pem;
ssl_certificate_key /usr/local/nginx/conf/cert/138.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#ssl证书结束
//配置站点的一起规则
location /
{
index index.php index.html index.htm admincp.php;
if (!-e $request_filename) {
rewrite ^/admincp.php/(.*)$ /admincp.php?$1 last;
rewrite ^(/index.php|/index.php/|/!admincp.php|/)(.*)$ /index.php?$1 last;
break;
}
}
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|a|jpeg|png|bmp|swf|mp3)$
{
expires 30d;
#add_header wall "hey!guys!give me a star.";
}
location ~ .*\.(js|css)?$
{
expires 12h;
#add_header wall "hey!guys!give me a star.";
}
access_log /home/wwwlogs/access.log;
}
演示效果如下:
注意站点是从server开始的,其它跟nginx.conf的配置是一样的,演示的是https的,http的去掉证书块的代码,监听端口修改为80.
3、监听多域名跳转到指定域名,配置如下:
代码如下:
server
{
#listen 80;
listen 443;
server_name www.test.club test.club;
#指定跳转开始
if ($host != 'www.test.club') {
rewrite ^/(.*)$ https://www.test.club/$1 permanent;
}
#指定跳转结束
index index.html index.php admincp.php;
root /mnt/test;
ssl on;
ssl_certificate /usr/local/nginx/conf/cert/107.club.pem;
ssl_certificate_key /usr/local/nginx/conf/cert/107.club.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location /
{
演示效果如下:
这样不同域名都可以打开同一个站点了,但最终显示的是同一个域名了,关键代码为:
if ($host != 'www.test.club') {
rewrite ^/(.*)$ https://www.test.club/$1 permanent;
}
注:配置好后,请重启 nginx服务器。
- 上一篇: Nginx 域名与证书配置详解
- 下一篇: 如何在 NGINX 中缓存内容
猜你喜欢
- 2025-01-21 30s 就可以掌握的 Nginx 片段
- 2025-01-21 Nginx域名配置
- 2025-01-21 Nginx配置终极手册:一站式详尽教程
- 2025-01-21 HHvm建站环境搭建方法:Nginx、lnmp/lamp等安装部署
- 2025-01-21 nginx修改conf后不生效的解决方法与root|alias总结
- 2025-01-21 前端项目中 浏览器缓存的更新不及时问题及解决方法
- 2025-01-21 Nginx反向代理:通过外网访问内网数据库(mysql)
- 2025-01-21 如何使用 Daphne + Nginx + supervisor部署 Django
- 2025-01-21 如何在Docker中打包部署Vue项目
- 2025-01-21 有遇到部署服务器后刷新404问题吗?
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- js判断是否空对象 (63)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- js数组插入 (83)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)