skip to Main Content

I am Trying to create two instance on my NGnix server

First would be accessed by

mydomain.com (it listening to port 80 )

Second using

172.32.32.123:81 (it listening to port 81 and this IP is server IP)

this is my default file

server {
    listen 80 default_server;
    listen [::]:80 default_server;


    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name mydomain.com;

    location / {

        try_files $uri $uri/ =404;
    }

        location ~ .php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    }


        location ~ /.ht {
        deny all;
    }
}
server {
    listen       81;
    server_name  172.32.32.123:81;
    root /var/www/html/root;
   index index.html index.php;
   set $MAGE_MODE developer; # or production or developer
   set $MAGE_ROOT /var/www/html/root/;

   location / {
           try_files $uri $uri/ =404;

   }
         location ~ .php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php7.0-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.1-fpm.sock;
    }

    include /var/www/html/root/nginx.conf.sample;

}
}

The server block is working fine for the one when using domain name but in case of IP based domain only home page is working on inner pages we are getting 404 error

3

Answers


  1. Chosen as BEST ANSWER

    Solved the issue with using following default config

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;
    
        root /var/www/html;
    
        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;
    
        server_name magedev.com;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            #try_files $uri $uri/ =404;
            index index.html index.php;
                    #try_files $uri $uri/  @handler;
            #try_files $uri $uri/ /index.php;
                    expires 30d;
        }
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
            location ~ .php$ {
            #include fastcgi_params;
            include snippets/fastcgi-php.conf;
        #
        #   # With php7.0-cgi alone:
        #   fastcgi_pass 127.0.0.1:9000;
        #   # With php7.0-fpm:
            fastcgi_pass unix:/run/php/php7.1-fpm.sock;
            #fastcgi_index index.php;
                #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
            location ~ /.ht {
            deny all;
        }
        #include /var/www/html/root/nginx.conf.sample;
    }
    server {
           listen 81;
           server_name 192.87.123.132;
    
           root /var/www/html/root;
           index index.html index.php;
         set $MAGE_MODE developer; # or production or developer
         set $MAGE_ROOT /var/www/html/root/;
    # **Inclusion of try_files Solved the  issue for me**
           location / {
                   try_files $uri $uri/ /index.php?$args;
                   autoindex on;
                   autoindex_exact_size off;
    
           }
                 location ~ .php$ {
                    include snippets/fastcgi-php.conf;
            #
            #       # With php7.0-cgi alone:
            #       fastcgi_pass 127.0.0.1:9000;
            #       # With php7.0-fpm:
                    fastcgi_pass unix:/run/php/php7.1-fpm.sock;
            }
    
        include /var/www/html/root/nginx.conf.sample;
    
    }
    

    Inclusion of try_files Solved the issue for me

       location / {
               try_files $uri $uri/ /index.php?$args;
               autoindex on;
               autoindex_exact_size off;
    
       }
    

  2. It’s unclear what’s supposed to happen — what is the correct path that’s supposed to handle one server versus the other?!

    If they’re supposed to have the same underlying files, then your root directives are quite suspicious — one is simply /var/www/html/, the other one is /var/www/html/root/ — is that intentional?

    Otherwise, in case of a 404 error, the underlying path names (that aren’t found) should be mentioned within the file specified by http://nginx.org/r/error_log, which will likely reveal what is up — do those returning 404 actually exist on the disc?!

    Login or Signup to reply.
    1. This would be better moved to ServerFault.

    2. While I’m not sure of it, The inclusion of the port in the ServerName directive looks suspicious to me.

    3. This looks dangerous:

      include /var/www/html/root/nginx.conf.sample

    If /var/www/html/root, and the directories above it are only writeable by root, it might be OK. Otherwise it’s likely to be a root exploit for whichever user can write to the file. Copy the file to somewhere safe, and include it at that location.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search