My nginx droplet can be visited at ip and domain name

January 15, 2015 4.5k views

I have tried few things and I am unable to have my ip redirect to my domain name. I tried redirecting in my sites-available but it breaks all my permalinks. Any ideas?

2 comments
  • What are you trying to do? Usually nginx uses virtual hosts ie: http://www.yourname.com to know which content to display.

  • so in the browser my site renders correctly with

    http://yourname.com

    and redirects http://www.yourname.com to -> http://yourname.com (all good)

    and when I drop my ip into the browser http://000.000.000.00/ my site is rendering but I would like it to redirect to http://yourname.com. This should be done with a rewrite? I tried whats in the wordpress codex:

    server {
            server_name  _;
            rewrite ^ $scheme://yourname.com$request_uri redirect;
    }
    

    no dice.

    here is my config:

    fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    
    server {
            server_name  _;
            rewrite ^ $scheme://yourname.com$request_uri redirect;
    }
    
    server {
        # Base info
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;
    
                # Initial location block for setup. if using wp super cache and in this case I am the 
                # try_files directive is replaced with supercache location block at the end.
                # the root and index directives are pulled out and placed just below. 
    
                # location / {
                        # root   /var/www/yoursite.com;
                        # index  index.php index.html index.htm;
    
                        # WordPress permalinks configuration
                        #   try_files $uri $uri/ /index.php?$args;
                # }
    
            root /var/www;
            index index.php index.html index.htm;
    
        # Make site accessible from http://localhost/
            server_name yourname.com;
    
      # Browser caching
            location ~*  \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
            expires 365d;
            }
    
          location ~*  \.(pdf)$ {
            expires 30d;
            }
    
        # Nginx Error Handling  
            error_page 404 /404.html;
            error_page 500 502 503 504 /50x.html;
            location = /50x.html {
                root /var/www;
            }
    
        # Base server side caching  
            location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_cache MYAPP;
            fastcgi_cache_valid 200 60m;
            }
    
        # Security
            # Block download agenta
             if ($http_user_agent ~* LWP::Simple|wget|libwww-perl) {
                    return 403;
             }
    
            # Block some nasty robots
             if ($http_user_agent ~ (msnbot|Purebot|Baiduspider|Lipperhey|Mail.Ru|scrapbot) ) {
                    return 403;
             }
    
          # Deny referal spam
             if ( $http_referer ~* (jewelry|viagra|nude|girl|nudit|casino|poker|porn|sex|teen|babes) ) {
                    return 403; 
             }
    
          # Prevent hotlinking
          location ~ .(gif|png|jpe?g)$ {
             valid_referers none blocked yourname.com *.yourname.com;
             if ($invalid_referer) {
                return   403;
            }
            }
    
        # WP Super Cache rules.
        # Based on http://codex.wordpress.org/Nginx
        # Designed to be included from a 'wordpress-ms-...' configuration file.
    
            set $cache_uri $request_uri;
    
            # POST requests and urls with a query string should always go to PHP
                if ($request_method = POST) {
                        set $cache_uri 'null cache';
                }
    
                if ($query_string != "") {
                        set $cache_uri 'null cache';
                }   
    
            # Don't cache uris containing the following segments
                if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
                        set $cache_uri 'null cache';
                }   
    
            # Don't use the cache for logged in users or recent commenters
                if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
                        set $cache_uri 'null cache';
                }
    
            # Use cached or actual file if they exists, otherwise pass request to WordPress
                location / {
                try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args ;
                }
    }
    
2 Answers

Yes you will have to create a new server block in the config file just to redirect/resolve IP address to domain name
Works good now thanks

Much like rewrite you are doing from www.yourname.com to yourname.com, you need to set a redirect from the IP to the domain name. You can use something like:

server {
    listen 80;
    server_name 111.11.111.111;

    return 301 $scheme://yourname.com$request_uri;
}
  • Thanks for taking a look.

    I just tried that solution and a few others and it works in the sense that it redirects correctly but it also throws my site into redirect loop. There must be something I am missing with the configuration. My google search keeps leading me to the same answers.

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!