Magento – Migrated from Apache to Nginx Except Homepage , Every Other Page shows 404

amazon-web-servicesmagento-1.9nginxphp-7

I am not a Developer so Please No Hate for fellow Human for asking this , I Was asked to setup :

An AWS-EC2 Instance of Ununtu 16.04
RDS With Default DB
LEMP Stack (Linux , Nginx , Magento , Php 7.0)

I followed : This Link

Now This Magento Site Was Developed in Apache2 Environment and Now We copied the files to /var/www/html , We almost solved all errors One by one , Except One.

Only Home Page Works , No other page will work it will give us 404 , Following are config files if helps :

Edit This is Update /etc/nginx/nginx.conf File`hint taken from answer and magento config :

server {
listen *:8080;
server_name http://my ec2 public DNS;
root /var/www/html/;

##
# redirect to www
##
#if ($host !~* ^www\.) {
#    rewrite ^(.*)$ http://www.$host$1 permanent;
#}

##
# dont log robots.txt requests
##
location /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

## These locations would be hidden by .htaccess normally
location ^~ /app/                { deny all; }
location ^~ /includes/           { deny all; }
location ^~ /lib/                { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/            { deny all; }
location ^~ /report/config.xml   { deny all; }
location ^~ /var/                { deny all; }
location /var/export/            { deny all; }
# deny htaccess files
location ~ /\. {
    deny  all;
    access_log off;
    log_not_found off;
}

##
# Rewrite for versioned CSS+JS via filemtime
##
location ~* ^.+\.(css|js)$ {
    rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last;
    expires 31536000s;
    access_log off;
    log_not_found off;
    add_header Pragma public;
    add_header Cache-Control "max-age=31536000, public";
}
##
# Aggressive caching for static files
# If you alter static files often, please use 
# add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
##
location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
    expires 31536000s;
    access_log off;
    log_not_found off;
    add_header Pragma public;
    add_header Cache-Control "max-age=31536000, public";
}

# error pages
error_page                    /404.html;

= /404.html {
root /usr/share/nginx/html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location / {
    try_files /maintenance.html $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
    expires 30d; ## Assume all files are cachable
}
location @handler { ## Magento uses a common front handler
    rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
    rewrite ^(.*.php)/ $1 last;
}

##
# pass the PHP scripts to FastCGI server listening at unix:/tmp/php5-fpm.sock
##
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SERVER_PORT 80;
    fastcgi_param  HTTPS $fastcgi_https;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
    fastcgi_param  MAGE_RUN_TYPE store;
}


rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;

}

Now the file in /etc/nginx/sites-avilable/default is :

Default server configuration

#
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 my server publlic ip;

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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.0-fpm.sock;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny all;
}

}

Virtual Host configuration for example.com

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

server_name my server ip;

root /var/www/html;
index index.php index.html;

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

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

location ~ /\.ht {
    deny all;
}

}

**Does Any One have any Idea why only home page opens ? Would Really Appreciate Help **

Best Answer

Assuming you have everything else set up correctly, your Nginx server block should probably be more like this, as a complete example. This case is using PHP-FPM though -

server {
listen      80;
server_name domain.com;
root        /home/someuser;
index index.html index.php;

access_log /var/log/nginx/domain.com-access_log;
error_log /var/log/nginx/domain.com-error_log;

location / {
    try_files $uri $uri/ @handler;
    expires 30d;
}
location /app/                       { deny all; }
location /includes/                  { deny all; }
location /lib/                       { deny all; }
location /media/downloadable/        { deny all; }
location /pkginfo/                   { deny all; }
location /report/config.xml          { deny all; }
location /var/                       { deny all; }

location /var/export/ {
    auth_basic              "Restricted";
    auth_basic_user_file    htpasswd;
    autoindex               on;
}
location  /. {
    return 404;
}

location @handler {
    rewrite / /index.php;
}

location ~ .php/ {
    rewrite ^(.*.php)/ $1 last;
}

location ~ \.php$ {
    try_files $uri =404;
    expires off;
    fastcgi_read_timeout 900s;
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
}
rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;

location /lib/minify/ {
    allow all;
}
gzip on;
#gzip_comp_level 9;
gzip_min_length  1000;
gzip_proxied any;
gzip_types       text/plain application/xml text/html text/css text/js application/x-javascript;

}