简体   繁体   中英

nginx is redirecting from one virtual host to another

I have a problem with my nginx configuration. I have installed an owncloud on the domain cloud.example.com and i want to have another site on example1.com. The site on example1.com is written with CakePHP and so I took the default nginx configuration for a CakePHP website. The Configuration for the owncloud i took from a german site and it worked just fine.

But now if I want to go to example1.com, i will moved permanently to cloud.example.com

Google Chrome says that: Status Code:301 Moved Permanently (from cache)
I cleared my browsercache and DNS-Cache multiple times.

config-files

example1.com

server {
    listen   80;
    server_name *.example1.com;
    return 301 http://example1.com$request_uri;
}
server {
    listen   80;
    server_name example1.com;

    # root directive should be global
    root   /var/www/example1/webroot;
    index  index.php;

    access_log /var/www/example1/log/access.log;
    error_log /var/www/example1/log/error.log;

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

    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Owncloud:

server_tokens off;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";

server {
    listen 80;
    server_name cloud.example.com;
    rewrite ^ https://$server_name$request_uri? permanent; # enforce https
}

server {
    listen 443 ssl;
    server_name cloud.example.com;
    ssl_certificate /etc/nginx/ssl/owncloud.crt;
    ssl_certificate_key /etc/nginx/ssl/owncloud.key;

        add_header Strict-Transport-Security max-age=31536000;


        root /var/www/cloud.example.com;
        index index.php;

        client_max_body_size 10G; # set max upload size
        fastcgi_buffers 64 4K;

        rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
        rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
        rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;

        error_page 403 = /core/templates/403.php;
        error_page 404 = /core/templates/404.php;

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
                deny all;
        }

        location / {
                rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
                rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

                rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
                rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;

                rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;

                try_files $uri $uri/ index.php;
        }

        location ~ ^(.+?\.php)(/.*)?$ {
                try_files $1 = 404;


                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$1;
                fastcgi_param PATH_INFO $2;
                fastcgi_param HTTPS on;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
        }

        location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
                expires 30d;
                # Optional: Don't log access to assets
                access_log off;
        }

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

        location ~ /\.ht {
                deny all;
        }
}

Update Okay - that makes a lot more sense... Try removing this return 301 http://example1.com$request_uri;

That's telling NGINX to return a 301 code for anything that comes in on the example1.com domain. https://en.wikipedia.org/wiki/HTTP_301 . Basically you're getting a "Moved Permanently" Error because you're telling the server to do exactly that ...;)

Old - no longer relevant since sample files changed Did you already try taking out the last server directive in your example.com config?

server {
    listen 80;
    server_name cloud.example.com;
    rewrite ^ https://$server_name$request_uri? permanent; # enforce https
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM