301 Moved Permanently error setting nginx

Discussion in 'Nginx' started by mstdmstd, Mar 13, 2019.

  1. #1
    Hello,
    I try to setup Multiple Domains in nginx under ubuntu 18
    with next steps :
    1) Create app directory
    mkdir /var/www/votes.com
    cd /var/www/votes.com
    sudo nano /var/www/votes.com/index.php
    Code (markup):
    with text
    File <b>/var/www/votes.com/index.php </b> index
    <?php 
    echo ‘Hello all!’;
    
    Code (markup):
    2) create /etc/nginx/sites-available/votes.com
    sudo nano /etc/nginx/sites-available/votes.com 
    with content :
    upstream votes-backend { # Lets you define a group of servers
       server unix:/var/run/php7.2-fpm.sock;
    }
    server {
       listen 80;
       root /var/www/votes.com;
       index index.php;
       server_name votes.com;
       #  rewrite ^ http://votes.com$request_uri? permanent; #301 redirect  TO UNCOMMENT
       # We keep this block, because it doesn't make sense to pass
       # everything to PHP.
       location / {
          try_files $uri $uri/ =404;
       }
       location ~ \.php$ {
         fastcgi_split_path_info ^(.+\.php)(/.+)$; # this defines a regular expression to separate the SCRIPT_FILENAME and PATH_INFO for later use
         try_files $uri =404; # OR Set cgi.fix_pathinfo=0 in php.ini (for security)
         fastcgi_pass votes-backend; # OR unix:/var/run/php7.2-fpm.sock OR 127.0.0.1:9000
         fastcgi_index index.php; # appends index.php to a URI ending with a slash
         include fastcgi_params;
       }
    }
    
    Code (markup):
    3) edit /etc/hosts
    add line :
    127.0.0.3   localhost votes.com
    Code (markup):
    4) create symbol link :
    sudo ln -s /etc/nginx/sites-available/votes.com /etc/nginx/sites-enabled/
    Code (markup):
    Check :
    cd /etc/nginx/sites-enabled/
    ls
    Code (markup):
    5) restart nginx service
    sudo service nginx restart
    sudo systemctl restart php7.2-fpm
    
    Code (markup):
    But restarting and refering the site
    curl votes.com
    I got error:
    $ sudo systemctl restart php7.2-fpm
    $ sudo service nginx restart
    ubuntu@ip-172-31-39-38:/etc/nginx/sites-enabled$ curl votes.com
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.10.1</center>
    </body>
    </html>
    
    Code (markup):
    and I uncomment the line above in config file and restarted services bit I got the same error
    again.
    How to fix it?
    Thanks!
     
    mstdmstd, Mar 13, 2019 IP
  2. hostechsupport

    hostechsupport Well-Known Member

    Messages:
    413
    Likes Received:
    23
    Best Answers:
    7
    Trophy Points:
    138
    #2
    What do you see in Nginx error logs? You may need to use HttpRewriteModule under Nginx web server...
     
    hostechsupport, Mar 13, 2019 IP
  3. bountysite

    bountysite Active Member

    Messages:
    71
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    73
    #3
    The curl command does not show the response headers. Use curl -v <vote.com>
    Try this config
    location / {
                      try_files $uri $uri/ /index.php index.php;
                  }
    
    
     
    location ~ \.php$ {
        try_files $uri =404;  
        fastcgi_pass votes-backend;  
    
        include fastcgi_params;
    
    }
      
    Code (ApacheConf):
     
    bountysite, Mar 13, 2019 IP
  4. mstdmstd

    mstdmstd Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Thank you for feedbacks!
    I modified my config file :
    upstream votes-backend { # Lets you define a group of servers
       server unix:/var/run/php7.2-fpm.sock;
    }
    server {
       listen 80;
       root /var/www/votes.com;
       index index.php;
       server_name votes.com;
       # rewrite ^ http://votes.com$request_uri? permanent; #301 redirect
       # We keep this block, because it doesn't make sense to pass
       # everything to PHP.
       location / {
          # try_files $uri $uri/ =404;
          try_files $uri $uri/ /index.php index.php;
       }
       location ~ \.php$ {
         fastcgi_split_path_info ^(.+\.php)(/.+)$; # this defines a regular expression to separate the SCRIPT_FILENAME and PATH_INFO for later use
         try_files $uri =404; # OR Set cgi.fix_pathinfo=0 in php.ini (for security)
         fastcgi_pass votes-backend; # OR unix:/var/run/php7.2-fpm.sock OR 127.0.0.1:9000
         fastcgi_index index.php; # appends index.php to a URI ending with a slash
         include fastcgi_params;
       }
    } 
    Code (markup):
    and restarting I got in console
    $ curl -v votes.com
    * Rebuilt URL to: votes.com/
    *   Trying 23.23.86.44...
    * TCP_NODELAY set
    * Connected to votes.com (23.23.86.44) port 80 (#0)
    > GET / HTTP/1.1
    > Host: votes.com
    > User-Agent: curl/7.58.0
    > Accept: */*
    >
    < HTTP/1.1 301 Moved Permanently
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Thu, 14 Mar 2019 04:32:00 GMT
    < Content-Type: text/html
    < Content-Length: 185
    < Connection: keep-alive
    < Location: http://www.votes.com/
    <
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.10.1</center>
    </body>
    </html>
    * Connection #0 to host votes.com left intact 
    Code (markup):
    in log files :
    /var/log/php7.2-fpm.log:
    [14-Mar-2019 04:31:49] NOTICE: Terminating ...
    [14-Mar-2019 04:31:49] NOTICE: exiting, bye-bye!
    [14-Mar-2019 04:31:49] NOTICE: fpm is running, pid 5048
    [14-Mar-2019 04:31:49] NOTICE: ready to handle connections
    [14-Mar-2019 04:31:49] NOTICE: systemd monitor interval set to 10000ms
    Code (markup):
    /var/log/nginx/access.log :
    -empty -
    /var/log/nginx/error.log :
    -empty-
     
    mstdmstd, Mar 13, 2019 IP
  5. bountysite

    bountysite Active Member

    Messages:
    71
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    73
    #5
    Your request for votes.com is going to a public internet server where it is hosted on. Hence you don't see anything in logs.

    Check /etc/hosts entry on the system, where you are running curl.
    It should point to the IP where nginx is running on.
     
    bountysite, Mar 13, 2019 IP
  6. mstdmstd

    mstdmstd Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #6
    I show my hosts file which has line :
    127.0.0.3 localhost votes.com
    Code (markup):
    Is it it? Must it be publick dns like created in freenom?
     
    mstdmstd, Mar 14, 2019 IP
  7. bountysite

    bountysite Active Member

    Messages:
    71
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    73
    #7
    Yes, check the IP address in curl.
    Are you using any system proxy?

    Try
    
    proxy=
    curl -v votes.com
    
    Code (markup):
    See what IP it connects to.
     
    bountysite, Mar 14, 2019 IP
  8. mstdmstd

    mstdmstd Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #8
    I am not sure which command have I to run ?
    In your script it looked like 2 commands :

    $ proxy=
    $ curl -v votes.com
    * Rebuilt URL to: votes.com/
    *  Trying 23.23.86.44...
    * TCP_NODELAY set
    * Connected to votes.com (23.23.86.44) port 80 (#0)
    > GET / HTTP/1.1
    > Host: votes.com
    > User-Agent: curl/7.58.0
    > Accept: */*
    > 
    < HTTP/1.1 301 Moved Permanently
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Thu, 14 Mar 2019 11:15:23 GMT
    < Content-Type: text/html
    < Content-Length: 185
    < Connection: keep-alive
    < Location: http://www.votes.com/
    < 
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.10.1</center>
    </body>
    </html>
    * Connection #0 to host votes.com left intact
    Code (markup):
    That is fresh ubuntu 18 installation under AWS
     
    mstdmstd, Mar 14, 2019 IP
  9. mstdmstd

    mstdmstd Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #9
    Sorry, no ideas what can help for me ?
     
    mstdmstd, Mar 14, 2019 IP
  10. mstdmstd

    mstdmstd Well-Known Member

    Messages:
    130
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #10
    Hello again,
    Looking at the article https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04
    I installed nginx under new ubuntu 18 installation under AWS and I want to make multiple domain names on 1 server. Fot this
    For this in AWS hosted zone in Route 53 console I created “sn.votes.demo.org” domain and I see https://prnt.sc/n1166swith domain names name servers at right.
    I suppose I have to use this domain name in my domain options.
    My /etc/nginx/sites-available/votes.com :
    
    upstream votes-backend { # Lets you define a group of servers
    server unix:/var/run/php7.2-fpm.sock;
    }
    server {
    listen 80;
    listen [::]:80;
    root /var/www/votes.com;
    index index.php;
    server_name mydomain // domein created above
    # We keep this block, because it doesn't make sense to pass
    # everything to PHP.
    location / {
    # try_files $uri $uri/ =404;
    try_files $uri $uri/ /index.php index.php;
    }
    location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$; # this defines a regular expression to separate the SCRIPT_FILENAME and PATH_INFO for later use
    try_files $uri =404; # OR Set cgi.fix_pathinfo=0 in php.ini (for security)
    fastcgi_pass votes-backend; # OR unix:/var/run/php7.2-fpm.sock OR 127.0.0.1:9000
    fastcgi_index index.php; # appends index.php to a URI ending with a slash
    include fastcgi_params;
    }
    }
    Code (markup):
    In /etc/hosts :
    127.0.0.1 localhost
    127.0.0.2 localhost test.com
    ec2-13-NN-NN-NN.us-east-2.compute.amazonaws.com mydomain
    # is it correct ?
    # The following lines are desirable for IPv6 capable hosts
    ::1 ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts
    
    Code (markup):
    Is it valid format ?
    I checked log files : /var/log/nginx/access.log, /var/log/nginx/error.log, /var/log/php7.2-fpm.log
    But did not find any errors.
    Opening url with my domain in browser I see quite different site untergrund.net opened. I have nothing with it.
    and I see ref to this site by console command wirth similar ref error:
    $ curl mydomain
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="http://www.untergrund.net">here</a>.</p>
    <hr>
    <address>Apache/2.2.15 (CentOS) Server at curl mydomain Port 80</address>
    </body></html>
    
    Code (markup):
    What is wrong ?
    Thanks!
     
    mstdmstd, Mar 23, 2019 IP