87709da3cb677ef93fa059d6665956910b3ebe66
By:
qmarlats

Nginx: multiple sites (including Wordpress) in multiple folders with droplet_ip/site_name as URL

June 6, 2015 2.6k views
Nginx LEMP WordPress DigitalOcean CMS Ubuntu

Hello,
I have a LEMP droplet on which I want to host few sites. For now I have two applications: PhpMyAdmin and a Wordpress site. But my Wordpress site is stored in "/srv/www/mysite/mysite/src" and I want to access to this site via the URL "x.x.x.x/mysite" (x.x.x.x is my droplet IP of course) because I can't use a domain name for the moment. So I tried a lot of things (with locations, aliases, root...) but none has worked. My last config file (works for PhpMyAdmin but not for "mysite"):

server {
    listen 80;
    listen [::]:80;

    root /srv/www;
    index index.php index.html;

    # Hosts
    server_name localhost x.x.x.x;

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

    location /mysite/ {
        alias /srv/www/mysite/mysite/src;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

As I said I've made a lot of tests and none of them has worked (there are always errors 404 and 500)... So thanks in advance for your help! :-)

6 Answers

You can do this over a symbol link

sudo ln -s /srv/www/mysite/mysite/src /srv/www
mv /srv/www/src /srv/www/mysite

If it works, reply

  • It doesn't work. I've added disable_symlinks off; in nginx.conf and of course I've renamed my "mysite" folder in "mysite-s" to allow to create a symbolic link named "mysite" in www. Then I've tried several things with my "location /mysite" (including removing it) and none of my tests works, I've always a 404.

It can't work if you rename the folder to mysite-s you need to copy and paste the code above

Here again

sudo ln -s /srv/www/mysite/mysite/src /srv/www
mv /srv/www/src /srv/www/mysite

Here is my configuration :

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;
    ssl_certificate_key /etc/nginx/ssl/ssl.key;
    ssl_certificate /etc/nginx/ssl/mail.crt;
    # Server Security SSL
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/ssl/dhparams.pem;
    ssl_session_cache shared:SSL:10m;
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    ssl_session_tickets off; # Requires nginx >= 1.5.9
    ssl_stapling on; # Requires nginx >= 1.3.7
    ssl_stapling_verify on; # Requires nginx => 1.3.7

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.php index.htm /_h5ai/server/php/index.php;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        autoindex on;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            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;
    #}
}

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}
  • If I don't rename the original folder, the symbolic link have the same name as this folder and so go in this folder instead of going to www. So the "src" of "mysite/mysite/src" go in "mysite/src" instead of www.

@qmarlats look how I have done it, you just need to change the source and destination, you need link it from the root of your Server /

http://imgur.com/1O6VnTO,4CVZ69J

  • To perfectly follow what you do:

    • I've movded mysite in /srv instead of /srv/www because your test folder is in /var and not in /var/www/html
    • So I've these folders: /srv/mysite (with mysite/src in mysite), and /srv/www (empty now)
    • sudo ln -s /srv/mysite/mysite/src /srv/www
    • mv /srv/www/ /srv/www/mysite It's nearly the same that change folder name (mysite to mysite-s), so no, it's not working.

    PS: For the moment I returned to Apache (my sites haven't a lot of traffic, so Apache is adequate for me ^^ ). If I want to use Nginx the only solution I've found is to use subdomains (because I already have domains of my sites). So this question is more for curiosity and not needed now, but thanks anyway a lot for your help! :-)

@qmarlats I know what you are doing wrong. You need to do this :

sudo ln -s /srv/mysite/mysite/src /srv/www
mv /src/www/src mysite

Now you can access example.com/mysite

  • No, I've made a small mistake in my previous answer, sorry. ^^ I've made mv /srv/www/src /srv/www/mysite and not mv /srv/www/ /srv/www/mysite. If I do what I told, I have a mysite folder in /srv/www with the Wordpress files, so it's right. But strangly, regardless how I configure Nginx, I've a 404.

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.php index.htm;

    server_name example.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        autoindex on;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            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;
    #}
}

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}

@qmarlats
Just change the root and the server name.

If you want I would give you my email address and configure your Droplet.

  • If I change the server_name and use a domain name, as I said it works. But my goal was to use my droplet IP instead of a domain name (in the case where I don't have a domain).

    I plan to migrate all my sites from OVH to Digital Ocean (Digital Ocean <3 ) and the first site I have to migrate will expire (hosting and domain) in few weeks. So really thank you for your help but I realize Nginx is pretty complicated compared to Apache for me. So for the moment I will use Apache which I know and later I'll retry Nginx on a non-production droplet... and if I've problems, I'll think of you, thanks! :-D

Have another answer? Share your knowledge.