为什么我的301重定向不能在nginx中运行?

时间:2016-05-06 23:39:51

标签: nginx

我有以下服务器块,我正在尝试进行301重定向,因此www.realestatelicensebystate.com会转到http://realestatelicensebystate.com进行搜索引擎优化。问题是,在我把线放到那里后,我得到了通用的“欢迎使用nginx”屏幕。这是代码:

    server {
            listen   80;
            server_name www.realestatelicensebystate.com;
            rewrite ^/(.*)$ http://realestatelicensebystate.com/$1 permanent;
            access_log /srv/www/realestatelicensebystate.com/logs/access.log;
            error_log /srv/www/realestatelicensebystate.com/logs/error.log;
            location / {
                    root   /srv/www/realestatelicensebystate.com/public_html;
                    index index.html index.htm index.php;
                    try_files $uri $uri/ /index.php?$args;
            }

            location ~ \.php$ {
                    include /etc/nginx/fastcgi_params;
                    #fastcgi_pass  127.0.0.1:9000;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME /srv/www/realestatelicensebystate.com/public_html$fastcgi_script_name;
            }
    }

一切都很突出,还是有什么我应该做得更好?我是nginx和学习的新手。

1 个答案:

答案 0 :(得分:0)

您获取默认nginx页面的原因在于:

server_name www.realestatelicensebystate.com;
rewrite ^/(.*)$ http://realestatelicensebystate.com/$1 permanent;

您的服务器正在侦听名称www.realestatelicensebystate.com,您正在重定向到realestatelicensebystate.com。

您需要为新名称创建另一个vhost配置或新服务器块。

server {
            listen   80;
            server_name www.realestatelicensebystate.com;
            rewrite ^ http://realestatelicensebystate.com$request_uri? permanent;
}

server {
            listen   80;
            server_name realestatelicensebystate.com;

            access_log /srv/www/realestatelicensebystate.com/logs/access.log;
            error_log /srv/www/realestatelicensebystate.com/logs/error.log;
            location / {
                    root   /srv/www/realestatelicensebystate.com/public_html;
                    index index.html index.htm index.php;
                    try_files $uri $uri/ /index.php?$args;
            }

            location ~ \.php$ {
                    include /etc/nginx/fastcgi_params;
                    #fastcgi_pass  127.0.0.1:9000;
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME /srv/www/realestatelicensebystate.com/public_html$fastcgi_script_name;
            }
    }