开发者社区> 问答> 正文

CentOSNGINX如何设置301

xx.com  301都www.xx.com
网上找了好几个都出错。

展开
收起
nojay 2014-08-05 13:05:52 6652 0
2 条回答
写回答
取消 提交回答
  • ReCentOSNGINX如何设置301
    网上找到的可能大部分都是if,但是官方建议尽量别使用if判断,你可以尝试下这个


    server {
            server_name  www.xxx.com;
            return       301 $scheme://xxx.com$request_uri;
    }

    server{
            listen    80;
            server_name xxx.com;
            root /b/domains/xxx.com;
            index index.php index.htm index.html;
            
            location ~ \.php$ {
                    fastcgi_pass   unix:/dev/shm/php.sock;
                    include        fastcgi_params;
                    fastcgi_param  SCRIPT_FILENAME  $document_root $fastcgi_script_name;
                    access_log     /a/apps/nginx/logs/xxx.com.access.log main;
            }
    }


    -------------------------

    ReCentOSNGINX如何设置301
    http://wiki.nginx.org/Pitfalls#Server_Name

    官方不建议用if的说法
    2014-08-06 09:13:03
    赞同 展开评论 打赏
  • ReCentOSNGINX如何设置301
    server{
        listen    80;
        server_name xxx.com www.xxx.com;
        root /b/domains/xxx.com;
        index index.html index.htm index.shtml index.php;
        error_page  404               /404.html;
        location = /500.html {
            root   /usr/share/nginx/html;
        }
        location ~ \.php$ {
            fastcgi_pass   unix:/dev/shm/php.sock;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            access_log     /a/apps/nginx/logs/xxx.com.access.log main;
        }

        location ~ /\.ht {
            deny  all;
        }
    }




    如何修改??

    -------------------------

    ReCentOSNGINX如何设置301
    if ($host != 'www.xxx.com' ) {
              rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
            }
    成功了
    2014-08-05 13:11:01
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载