1

I have the following nginx config file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#


server {

    root /var/www/open_final/current;     
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name app.mypersonaldomain.co;
        if ($http_x_forwarded_proto != "https") {
          rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
        }

#        if ($http_user_agent ~* '(iPhone|iPod|android|blackberry)') {
#         return 301 https://mobile.mypersonaldomain.co;
#        }
    location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
        #       try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.html;
        # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
}


server {

    root /var/www/open-backend-v2/current/public;
    index index.php index.html index.htm;

    server_name localhost v2-api.mypersonaldomain.co;

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

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

I have two applications running on this nginx server. One is a Laravel(PHP) application and an Angular application (Front-end) running. I have noticed that last week, all the backend application (PHP) routes started throwing 404 Not Found errors. I restarted my nginx, still it was coming. Finally I restarted my aws instance and it started working fine. Again yesterday all of a sudden , the URLs started throwing 404 all of a sudden and I had to restart the instance.

The front-end application was loading but the backend (Laravel-PHP) urls was throwing 404.

I suspect if its some hacker doing it. In the past 2 years this was not happening and it started coming from last week.

What could be the reason for it? Is it like someone tampering the .htaccess file or is it something to do with nginx config. But if so why on the laravel application routes are showing 404.

Need help on this. What could be the reason for this ? Has anyone faced this issue ?

  • Can you identify the exact time when the server suddenly started responding 404? Find your NginX log files and look for them. Also in NginX we always say "IF IS EVIL". If you want your visitors to be transfared from HTTP to HTTPS use this code:xolphin.com/support/Nginx/Nginx_-_Redirect_HTTP_to_HTTPS – Bert Jan 22 '19 at 9:54
0

Is everything OK with the PHP FPM service? Could be that this service crashes and comes back after the reboot.

Check the status of the service if you get all the 404s and also the logs

| improve this answer | |
0

I see you have mixed 2 domains in one nginx.conf. This is very messy because now I have no clue what do you want to achieve. I use NginX on many of my webservers, most of them have more than one website served. On these, I have a good method that is serving me fine for years, let me share it with you so you can have a cleaner look.

Firstly separate nginx.conf from your website.conf. Create /etc/nginx/sites-available and /etc/nginx/sites-enable. In the nginx.conf I remove all server blocks, so I know only websites I enable are served, nothing else. Keep only the http and events block and everything else there like user. Comment out your include line in the http block and add a new line according to this:

        #include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*.conf;

Now create a new config file /etc/nginx/sites-available/co.mypersonaldomain.mobile.conf. Add two server blocks. The 1st one (if you want to forward all HTTP to HTTPS should be exactly this:

server {
       listen         80;
       server_name    app.mypersonaldomain.co;
       return         301 https://$server_name$request_uri;
}

The 2nd server block needs to be set up to your needs, I can't really help in that one. But from your config I can tell you you can skip the error pages and the location for the error pages as well, unless you are using a custom built. The rest is OK. Also, a huge recommendation is to use custom log files, not the standard NginX logs, so you can easily make a difference between your websites on your server and easier to maintain. Add these lines to your 2nd server block

    error_log               /var/log/nginx/co.mypersonaldomain.mobile/error.log;
    access_log              /var/log/nginx/co.mypersonaldomain.mobile/access.log main;

After this, you can simply link this config file to the sites-enabled folder with this command: ln -s /etc/nginx/sites-available/co.mypersonaldomain.mobile.conf /etc/nginx/sites-enable. Also, do not forget to create the folder for the log files: mkdir /var/log/nginx/co.mypersonaldomain.mobile/.

Lastly, you can test the NginX configs befoore you activate them with nginx -t. If this sais all syntax is correct, then just restart your NginX and voila.

My issue with your goal is that v2-api.mypersonaldomain.co domain name. It is clear that you are trying to use that as the backend and use API for it, and your config looks cool enough, just create a separate config for it as well in /etc/nginx/sites-available/co.mypersonaldomain.v2-api.conf and create it for yourself. However, your are redirecting the users from http://app.mypersonaldomain.co to https://app.mypersonaldomain.co but that domain is not served at all in your current config.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.