Question

Application is not calling by NGINX

Posted May 4, 2021 838 views
NginxCentOSPythonDjangoPython FrameworksDeployment

As I was followed your tutorial on how to configure NGINX + Django. However, at last I was not able to see my application om browser. I am still getting message “Welcome to nginx!” on my browser. Currently I am using Centos7.5. Kindly let me know that how to trace the errors or steps to overcome this issue.

Thanks in advance. Regards, Venu

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi there,

It is possible that you have multiple Nginx server block files in /etc/nginx/conf.d and the incorrect one is being served.

You can check that with the following command:

sudo ls -l /etc/nginx/conf.d

If this is the case I would recommend removing the one that is not needed.

If there is only one file in there, then it could be a problem with the configuration. Feel free to share the content of the file here so that I could try to advise you further.

Regards,
Bobby

  • Hi Bobby, Many thanks for your swift reply. This is first time I am working on deploying Django using NGINX.

    As you said I have checked “sudo ls -l /etc/nginx/conf.d” and the reponse is “total 4 -rw-r–r– 1 root root 1077 May 4 13:59 default.conf”

    In folder “/etc/nginx/conf.d” there is only one file default.conf

    I have included below lines in defualt.conf

    #server {
     #   listen       80;
      #  server_name  localhost;
    
        #access_log  /var/log/nginx/host.access.log  main;
    
       # location / {
        #    root   /usr/share/nginx/html;
         #   index  index.html index.htm;
        #}
    
    server {
        listen 80;
        server_name 103.108.220.46;
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/Dev_User_1/InvestmentProject/InsuranceTrackerV44;
        }
    
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock;
        }
    }
    
    
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    

    However, InsuranceTracker.sock is not there in my file structure.

    Regards,
    Venu

    edited by MattIPv4
    • Hi there,

      This looks quite OK, what I could suggest is to run an Nginx configuration test with the following command:

      sudo nginx -t
      

      If you get any errors feel free to share them here, if not you could try restarting Nginx:

      sudo systemctl restart nginx
      

      Let me know how it goes.
      Regards,
      Bobby

      • Hi Bobby,

        Here is the error after executed the command sudo nginx -t

        nginx: [emerg] “location” directive is not allowed here in /etc/nginx/conf.d/default.conf:37
        nginx: configuration file /etc/nginx/nginx.conf test failed

        • Hi there,

          Ah yes, so on line 37 you need to delete the location directive and then run the config test again.

          I would recommend removing all of the not-needed content from the configuration file and leaving just the configuration that you need:

          server {
              listen 80;
              server_name 103.108.220.46;
          
              location = /favicon.ico { access_log off; log_not_found off; }
              location /static/ {
                  root /home/Dev_User_1/InvestmentProject/InsuranceTrackerV44;
              }
          
              location / {
                  proxy_set_header Host $http_host;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_set_header X-Forwarded-Proto $scheme;
                  proxy_pass http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock;
              }
          }
          

          Note that once you get Syntax OK you would need to restart the Nginx service to apply the new changes.

          Let me know how it goes.
          Regards,
          Bobby

          • Hello Bobby, Thank you very much for help and support. As you suggested, I have modified default.conf file. Now I could able run commands

            sudo nginx -t
            nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
            nginx: configuration file /etc/nginx/nginx.conf test is successful

            and successfully executed sudo systemctl restart nginx

            However, I am getting 502 Bad Gateway error in my browser.

            I am getting below error log when I executed below command.
            sudo tail -30 /var/log/nginx/error.log

            2021/05/08 11:40:01 [crit] 7197#7197: *4 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 103.108.220.46, request: "GET /whm-server-status HTTP/1.0", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/whm-server-status"
            2021/05/08 11:45:01 [crit] 7197#7197: *6 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 103.108.220.46, request: "GET /whm-server-status HTTP/1.0", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/whm-server-status"
            2021/05/08 11:47:23 [crit] 7198#7198: *8 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 103.108.220.46, request: "GET / HTTP/1.0", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/", host: "localhost"2021/05/08 11:48:16 [crit] 7197#7197: *10 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 223.230.43.194, server: 103.108.220.46, request: "GET / HTTP/1.1", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/", host: "103.108.220.46"2021/05/08 11:50:02 [crit] 7198#7198: *13 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 103.108.220.46, request: "GET /whm-server-status HTTP/1.0", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/whm-server-status"2021/05/08 11:53:06 [crit] 7197#7197: *15 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 103.108.220.46, request: "GET / HTTP/1.0", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/", host: "localhost"
            2021/05/08 11:53:19 [notice] 7196#7196: signal 15 (SIGTERM) received from 12146, exiting
            2021/05/08 11:53:19 [notice] 7198#7198: exiting
            2021/05/08 11:53:19 [notice] 7198#7198: signal 15 (SIGTERM) received from 1, exiting
            2021/05/08 11:53:19 [notice] 7198#7198: exit
            2021/05/08 11:53:19 [notice] 7197#7197: signal 15 (SIGTERM) received from 1, exiting
            2021/05/08 11:53:19 [notice] 7197#7197: exiting
            2021/05/08 11:53:19 [notice] 7197#7197: exit
            2021/05/08 11:53:19 [notice] 7196#7196: signal 15 (SIGTERM) received from 1, exiting
            2021/05/08 11:53:19 [notice] 7196#7196: signal 17 (SIGCHLD) received from 7198
            2021/05/08 11:53:19 [notice] 7196#7196: worker process 7198 exited with code 0
            2021/05/08 11:53:19 [notice] 7196#7196: signal 29 (SIGIO) received
            2021/05/08 11:53:19 [notice] 7196#7196: signal 17 (SIGCHLD) received from 7197
            2021/05/08 11:53:19 [notice] 7196#7196: worker process 7197 exited with code 0
            2021/05/08 11:53:19 [notice] 7196#7196: exit
            2021/05/08 11:53:19 [notice] 12150#12150: using the "epoll" event method
            2021/05/08 11:53:19 [notice] 12150#12150: nginx/1.20.0
            2021/05/08 11:53:19 [notice] 12150#12150: built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
            2021/05/08 11:53:19 [notice] 12150#12150: OS: Linux 3.10.0-1160.25.1.el7.x86_64
            2021/05/08 11:53:19 [notice] 12150#12150: getrlimit(RLIMIT_NOFILE): 1024:4096
            2021/05/08 11:53:19 [notice] 12151#12151: start worker processes
            2021/05/08 11:53:19 [notice] 12151#12151: start worker process 12152
            2021/05/08 11:53:19 [notice] 12151#12151: start worker process 12153
            2021/05/08 11:53:24 [crit] 12153#12153: *1 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 66.249.64.218, server: 103.108.220.46, request: "GET /%E0%A4%AA%E0%A4%BE%E0%A4%95%E0%A4%BF%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A4%BE%E0%A4%A8-%E0%A4%AE%E0%A5%87%E0%A4%82-%E0%A4%87%E0%A4%82%E0%A4%B8%E0%A4%BE%E0%A4%A8%E0%A5%8B%E0%A4%82-%E0%A4%B8%E0%A5%87/ HTTP/1.1", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/%E0%A4%AA%E0%A4%BE%E0%A4%95%E0%A4%BF%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A4%BE%E0%A4%A8-%E0%A4%AE%E0%A5%87%E0%A4%82-%E0%A4%87%E0%A4%82%E0%A4%B8%E0%A4%BE%E0%A4%A8%E0%A5%8B%E0%A4%82-%E0%A4%B8%E0%A5%87/", host: "ourplatforms.com"
            2021/05/08 11:55:01 [crit] 12153#12153: *3 connect() to unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: 103.108.220.46, request: "GET /whm-server-status HTTP/1.0", upstream: "http://unix:/home/Dev_User_1/InvestmentProject/InsuranceTrackerV44/InsuranceTracker.sock:/whm-server-status"
            
            edited by MattIPv4
          • Hi there @venukadalinv,

            Now that Nginx is up and running it looks like that the Django application is not running so Nginx is unable to connect and is throwing the 502 error.

            Here is a video describing the problem but with another backend application:

            What you need to do is to start the Django application and if it fails to start, you need to check the logs to see why it is not starting.

            You can follow the steps on how to troubleshoot Django in the last step of this article here:

            https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04

            Let me know how it goes!
            Regards,
            Bobby

            by Justin Ellingwood
            Django is a powerful web framework that can help you get your Python application or website off the ground. Django includes a simplified development server for testing your code locally, but for anything even slightly production related, a more secure and powerful web server...