简体   繁体   中英

Nginx Running Wordpress Redirecting www to non-www by mistake

I have an nginx server (1.4.1) running a website in Wordpress. It is automatically redirecting www.mysite.com to mysite.com without any specific rules for doing so. I'd like it to stop and keep the www if that was originally entered.

Can you please examine my configuration and let me know what might be the cause? If I test it on image files, the redirect does not occur, but on php files it does...

server {
    listen   80;
    server_name www.mysite.com mysite.com;
    access_log /logs/mysite.com/access.log;
    error_log /logs/mysite.com/error.log crit;
    root   /home/sites/mysite.com/;

    location / {
        index index.php;
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_intercept_errors off;
    }
}

fastcgi_params:

fastcgi_param   QUERY_STRING            $query_string;
fastcgi_param   REQUEST_METHOD          $request_method;
fastcgi_param   CONTENT_TYPE            $content_type;
fastcgi_param   CONTENT_LENGTH          $content_length;

fastcgi_param   SCRIPT_FILENAME         $request_filename;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
fastcgi_param   REQUEST_URI             $request_uri;
fastcgi_param   DOCUMENT_URI            $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL         $server_protocol;

fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;

fastcgi_param   REMOTE_ADDR             $remote_addr;
fastcgi_param   REMOTE_PORT             $remote_port;
fastcgi_param   SERVER_ADDR             $server_addr;
fastcgi_param   SERVER_PORT             $server_port;
fastcgi_param   SERVER_NAME             $server_name;

fastcgi_param   HTTPS                   $https;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS         200;

I think your issue isn't nginx, It's a wordpress config, go to wordpress settings and make sure your website URL doesn't have www. check http://yourdomain.com/wp-admin/options-general.php

Doesn't this line say that it should use both as the correct address of your site?

server_name www.mysite.com mysite.com;

Try using this configuration, it will cause all traffic for any subdomain and no subdomain to go to the www.mysite.com counterpart.

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

server {
    listen          80;
    server_name     www.mysite.com;

    index           index.html;
    root            /home/mysite.com;
}

Oh, don't just copy/past, adjust it to your serversettings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM