最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】实现非www的dayun.it跳转到www域名www.dayun.it

跳转 crifan 893浏览 0评论
之前:
【已解决】OneinStack中安装配置虚拟主机域名
虽然实现了主页:
http://xxx
但是访问非www的:
http://xxx
还是会访问到访问IP才能看到的主页,显示的OneinStack:
Welcome to use OneinStack
不过访问:
https://xxx
会自动跳转到:
https://xxx
所以感觉是:
用OneinStack新建vhost时,除了www的xxx,没有添加非xxx
根据前段时间的经验,而此处想要解决,应该是去修改nginx配置。
所以去看看
[root@VM_0_10_centos ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
所以去看看配置文件:
/usr/local/nginx/conf/nginx.conf
  server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default;
    index index.html index.htm index.php;
    ...
########################## vhost #############################
  include vhost/*.conf;
}
然后再去看看vhost的配置:
[root@VM_0_10_centos conf]# cd vhost/
[root@VM_0_10_centos vhost]# ll
total 4
-rw-r--r-- 1 root root 1217 Dec  5 22:12 xxx.conf
[root@VM_0_10_centos vhost]# sz xxx.conf 
rz
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring xxx.conf...
  100%       1 KB       1 KB/sec    00:00:01       0 Errors  
下载下来后,去看看配置:
server {
  listen 80;
  listen 443 ssl http2;
...
  server_name xxx;
  index index.html index.htm index.php;
  root /data/wwwroot/xxx;
很明显是:
80的http和443的https
xxx
都是可以打开主页的
-》所以再去加上:
server_name xxx xxx;
上传上去:
[root@VM_0_10_centos vhost]# mv xxx.conf xxx.conf_backup
[root@VM_0_10_centos vhost]# rz
rz waiting to receive.
Starting zmodem transfer.  Press Ctrl+C to cancel.
Transferring xxx.conf...
  100%       1 KB       1 KB/sec    00:00:01       0 Errors  

[root@VM_0_10_centos vhost]# ll -lha
total 16K
drwxr-xr-x 2 root root 4.0K Dec  7 21:16 .
drwxr-xr-x 5 root root 4.0K Dec  5 22:12 ..
-rw-r--r-- 1 root root 1.2K Dec  7 21:15 xxx.conf
-rw-r--r-- 1 root root 1.2K Dec  5 22:12 xxx.conf_backup
然后再去重新加载nginx:
[root@VM_0_10_centos vhost]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@VM_0_10_centos vhost]# service nginx reload
Redirecting to /bin/systemctl reload  nginx.service
[root@VM_0_10_centos vhost]# service nginx status
Redirecting to /bin/systemctl status  nginx.service
[0m nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-12-06 22:10:48 CST; 23h ago
     Docs: http://nginx.org/en/docs/
  Process: 23309 ExecReload=/usr/local/nginx/sbin/nginx -s reload (code=exited, status=0/SUCCESS)
  Process: 2075 ExecStartPost=/bin/sleep 0.1 (code=exited, status=0/SUCCESS)
  Process: 2065 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 1888 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 2073 (nginx)
   CGroup: /system.slice/nginx.service
            2073 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           23311 nginx: worker process

Dec 06 22:10:47 VM_0_10_centos systemd[1]: Starting nginx - high performance web server...
Dec 06 22:10:48 VM_0_10_centos nginx[1888]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
Dec 06 22:10:48 VM_0_10_centos nginx[1888]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Dec 06 22:10:48 VM_0_10_centos systemd[1]: Started nginx - high performance web server.
Dec 07 21:17:26 VM_0_10_centos systemd[1]: Reloading nginx - high performance web server.
Dec 07 21:17:26 VM_0_10_centos systemd[1]: Reloaded nginx - high performance web server.
然后再去访问:
xxx
然后就可以跳转到www的地址:
http://xxx/
-》且自动又用了https:
【总结】
此处OneinStack添加vhost的xxx,以及加了https
之后访问:
http://xxx
却只是OneinStack的页面
想要访问:
http://xxx
自动跳转到:
http://xxx
的解决办法是:
修改对应的vhost的配置文件:
/usr/local/nginx/conf/vhost/xxx.conf
server_name加上xxx:
server {
  listen 80;
  listen 443 ssl http2;
...
  server_name xxx xxx;
即可。
注意重新加载最新配置:
service nginx reload

转载请注明:在路上 » 【已解决】实现非www的dayun.it跳转到www域名www.dayun.it

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.199 seconds, using 21.99MB memory