首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在多域环境下将nginx中的非www重定向到www

问如何在多域环境下将nginx中的非www重定向到www
EN

Stack Overflow用户
提问于 2017-04-18 16:11:11
回答 1查看 283关注 0票数 0

我有多个域名注册在服务提供商和托管与nginx的linode。

域名示例为www.example.com (非example.com)

所有的域名都可以很好地使用www,但如果没有它,页面将无法加载。

代码语言:javascript
复制
In curl -I http://example.com 

it结果

代码语言:javascript
复制
curl: (6) Could not resolve host http://example.com

我分别有3个A/AAAA记录

代码语言:javascript
复制
blank    IP
mail     IP
www      IP

在我的/etc/nginx/sites-available

代码语言:javascript
复制
server {
        listen 80;
        listen [::]:80;
        server_name example.com www.example.com; 
        access_log /var/log/nginx/example.com.log;
        error_log /var/log/nginx/example.com-error error;

        root /var/www/examplecom/public;
        index index.html index.php;

        location ~ \.php?$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php-main.socket;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors on;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            #Prevent version info leakage
            fastcgi_hide_header X-Powered-By;
        }
}

server {
    listen 80;
    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

我也尝试过在顶部编写第二个服务器块,但结果相同。

请立即提供指导/帮助。

EN

回答 1

Stack Overflow用户

发布于 2017-04-19 14:20:23

感谢你们的回复。

顺便说一下,当我在linode设置中将我的域名从www.example.com更改为example.com时,这个问题就解决了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43466956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档