P
P
pingo2016-02-19 17:28:08
Nginx
pingo, 2016-02-19 17:28:08

Nginx: Why does it redirect from the main one domain to another?

there is domain1.com on the server, it hangs on nginx + php5-fpm
Added another ip to the server,
set up dns for the second domain, resolves correctly,
registered the config in /sites-available/domain2.com,
linked with /sites-enabled
why _ domain2.com/index.html works, but _ domain2.com redirects to
_ domain1.com is a bastard. I can’t understand
, well, yes, rebutyl, but the configs are standard, like, there’s nothing special to change there

server {
   listen 80;

   root /var/www/user1/data/www/domain1.com;
   index index.html index.htm index.php;

   server_name domain1.com;

     location / {
       try_files $uri $uri/ /index.php?$query_string;
       if ($http_host ~ "^115\.1\.1\.1"){ rewrite ^(.*)$ http://domain1.com$1 redirect; }
    }

server {
   listen 80;

   root /var/www/user2/data/www/domain2.com;
   index index.html index.htm index.php;

   server_name domain2.com;

     location / {
       try_files $uri $uri/ /index.php?$query_string;
       if ($http_host ~ "^115\.2\.2\.2"){ rewrite ^(.*)$ http://domain2.com$1 redirect; }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xbox, 2016-02-26
@xbox

Why do you need this garden with a check through if and redirects?
If constructs in nginx configs are not recommended. They can slow down the system and you can usually do without them.
If I understood correctly, then you have two domains and each should respond only to its own IP address. Now you have both configs configured so that they listen on any ip address.
And you need to do this:

server {
   listen 115.1.1.1:80;
   server_name domain1.com;

   access_log /var/www/logs/nginx-domain1.access.log combined;
   error_log  /var/www/logs/nginx-domain1.error.log;

   root /var/www/user1/data/www/domain1.com;
   index index.html;

     location / {
       try_files ...;
     }

    }

server {
   listen 115.2.2.2:80;
   server_name domain2.com;

   access_log /var/www/logs/nginx-domain2.access.log combined;
   error_log  /var/www/logs/nginx-domain2.error.log;

   root /var/www/user2/data/www/domain2.com;
   index index.html;

     location / {
       try_files ...;
     }

    }

The main thing in this example each domain only responds to its own IP address.
If the DNS records in the domain settings are correctly configured, then each domain sends requests to only one IP address anyway.
Additionally, a separate log file is specified for each domain.
See what will be written in the logs. If the usual log does not help, then try turning on debug mode for error_log.
The error log can collect extended information about server operation. This is handy when debugging and finding out why:
error_log /var/log/nginx/error.log debug;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question