3

i'm new to nginx and i have a problem with virtual host. The virtual host didn't work when i try to access the vhost it'll be redirect to localhost "Welcome to nginx". Here are the contents of my config:

/etc/hosts config:

127.0.0.1 localhost localhost.localdomain

::1 localhost localhost.localdomain

****Generated by Admin****

18.200.10.50 mail.testingweb.com

18.200.10.50 testingweb.com

SSL config on /etc/nginx/conf.d/ssl.conf:

server {
       listen 443 default_server ssl;
       server_name testingweb.com;

       ssl_certificate /etc/nginx/sslcert/xxxx.crt;
       ssl_certificate_key /etc/nginx/sslcert/xxxxx.key;

       ssl_session_cache shared:SSL:10m;
       ssl_session_timeout 10m;
       keepalive_timeout 70;

       ssl_prefer_server_ciphers on;
       ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNU$
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

       location / {
               root /usr/share/nginx/html;
               index index.php index.html index.htm;
       }

        location ~ \.php$ {
                try_files $uri =404;

                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}

/etc/nginx/sites-available/default config:

server {
        listen 80 default_server;
        # listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/xhtml;
        index index.php index.html index.htm;


        # Make site accessible from http://localhost/
        server_name testingweb.com;
        return 301 https://$host$request_uri;

        location / {
                try_files $uri $uri/ =404;
        }
        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;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                try_files $uri =404;

        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$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;
        }
}
server {
        listen 80;
        listen 443;
        return 403;
}

I want to access another sites from new root directory, /usr/share/nginx/html/www on www directory there is a wordpress.

/etc/nginx/sites-available/testingweb config:

server {
        listen 80 default_server;
        # listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html/www;
        index index.php index.html index.htm;


        # Make site accessible from http://localhost/
        server_name testingweb.com;

#       rewrite ^ https://$http_host$request_uri? permanent;
        return 301 https://$host$request_uri;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?q=$uri&$args;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules;
        }


   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;
    }

        location ~ \.php$ {
                try_files $uri =404;

        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

#       location = /favicon.ico {
#               alias /usr/share/nginx/html/favicon.ico;
#       }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

According the configs, what's wrong with my config ? i cannot access the wordpress file on /usr/share/nginx/html/www directory by domain testingweb.com ? its always redirect to default host instead of testingweb host ?

sorry for my bad english..

10
  • Both your default and testingweb configurations are returning 301 redirects in the middle of the block, before even proxying requests to php-fpm. Is your nginx working at all? Commented Apr 4, 2015 at 18:47
  • Yes, the server (nginx) is working fine. So should i remove the returning 301 on 'testingweb' ? Commented Apr 5, 2015 at 0:36
  • You only need the return directive if you need to redirect a request to another URL -- so if you want the requests that end up in your default server block to go to testingweb where your WordPress setup is, then keep the return in the default config. But you don't need it in testingweb if you have rules (location blocks) to handle the request. Commented Apr 5, 2015 at 0:44
  • is your testingweb configuration working OK for your WordPress site? Commented Apr 5, 2015 at 0:45
  • @kchan ok, i'll try it later. yes, i can access Wordpress sites over testingweb.com/www instead of testingweb.com Commented Apr 5, 2015 at 2:06

1 Answer 1

3

This is a revised version of the nginx configuration from your pastebin code:

server {
        listen 80;
        # listen [::]:80 default_server ipv6only=on;
        # Make site accessible from http://devdev.com/
        server_name devdev.com;
        return 301 https://$host$request_uri; 
}

# HTTPS server
#
server {
        listen 443 default_server ssl;
        server_name devdev.com;
        root /var/www;
        index index.php index.html index.htm;

        # uncomment to add your access log path here
        # access_log /var/log/nginx/devdev.com.access.log main;

        ssl_certificate /etc/ssl/ssl-unified.crt;
        ssl_certificate_key /etc/ssl/ssl-my-private-decrypted.key;

        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;
        keepalive_timeout 70;

        ssl_prefer_server_ciphers on;
        ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS +RC4 RC4";
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        location @default {
                rewrite ^/(.*) /index.php?uri=$request_uri last;
        }

        location / {
                try_files $uri $uri/index.php @default;
        }

        location ~ \.php$ {
                try_files $uri =404;

                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}

The first server block listening on port 80 just redirects to https://devdev.com/. This will redirect all http requests to https so you don't need any other processing rules.

The second server block listens on port 443 and will proxy requests with a path ending with .php to php-fpm (you want to double-check that it's running on a unix socket and your permissions are correct).

The location block matching the / prefix (location /) will try to match files in the request URI and handle the request appropriately. For example:

  • If the request is for /index.php and the file exists, the following block will match the .php suffix and proxy to php-fpm.
  • If the request is for /foo and there's no match for a file by that name, nginx will try to match /foo/index.php and then proxy to php-fpm.
  • If there is still no match, try_files will use the @default location block, which just sends the request to your top-level /index.php with the request URI as parameters.

If your WordPress site is located in /var/www -- the top-level entry point should be /var/www/index.php -- this configuration should work. You might need to tweak the configurations based on your WordPress settings -- though this is generic enough that it should work without a lot of changes.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the config, but its still didn't work, when i access (http://)devdev.com its always redirect to index.html from /usr/share/nginx/html..
The problem was solved. Its caused by wrong location / { .. } in SSL server block. Thank you so much for your help @kchan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.