Myth494
By:
Myth494

NGINX have two url address locations on single domain

August 5, 2016 2.6k views
MariaDB Nginx LEMP CentOS

Hi, i'm fairly new to NGINX.. well Linux in general actually. I've managed to successfully set up TechnicSolder on my VM and using "Domain.com" works as would be expected. But i would like to have TechnicSolder using the url of "Domain.com/solder" so i can then have a simple site on 'Domain.com" what is the basic way for me to change this?

i installed NGINX via this guide: https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7

i've tried adding another server block, tried adding another location, but i'm a bit in the dark on how to get two addresses on the one domain, if possible i would rather not have a sub-domain

I essentially have this in my conf.d file (this is just a copy from solder's example):

server {
    listen 80; #IPv4
    listen [::]:80; #IPv6

    # Host that will serve this project.
    server_name     solder.technicpack.net;

    # Useful logs for debugging.
    access_log      /var/log/nginx/solder.technicpack.com/access.log;
    error_log       /var/log/nginx/solder.technicpack.com/error.log;
    rewrite_log     on;

    # The location of our projects public directory.
    root            /var/www/solder.technicpack.com/htdocs/TechnicSolder/public;
    index index.php;

    location / {
        # URLs to attempt, including pretty ones
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # PHP FPM configuration.
    location ~* \.php$ {
            fastcgi_pass                    unix:/var/run/php5-fpm.sock;
            fastcgi_index                   index.php;
            fastcgi_split_path_info         ^(.+\.php)(.*)$;
            include                         fastcgi_params;
            fastcgi_param PATH_INFO         $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
            deny all;
    }

    # Hide dot files/folders
    location ~ .*/\. {
            return 403;
    }

    # Set header expirations on per-project basis
    location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
            expires 365d;

    }
}

Thank you for your time in reading this
Mytyh494

2 Answers

You can simply move the files into the a different sub-directory. For example, your domain.com will load files in/var/www/solder.technicpack.com/htdocs/TechnicSolder/public. Domain.com/solder will load files in /var/www/solder.technicpack.com/htdocs/TechnicSolder/public/solder.

So you have to create a directory named solder in var/www/solder.technicpack.com/htdocs/TechnicSolder/public/ and place TechnicSolder files in this folder.

  • is it possible to have them in two different directory's? so var/www/solder.technicpack.com/htdocs/TechnicSolder/public/ and var/www/solder.technicpack.com/another/example/location

Hi,

You can configure your server block to serve your site on domain.com, and then host TechnicSolder in a subdirectory.

server {
    listen 80; #IPv4
    listen [::]:80; #IPv6

    # Host that will serve this project.
    server_name     solder.technicpack.net;

    # Useful logs for debugging.
    access_log      /var/log/nginx/solder.technicpack.com/access.log;
    error_log       /var/log/nginx/solder.technicpack.com/error.log;
    rewrite_log     on;

    # The location of our projects public directory.
    root            /var/www/path/to/simple/site;
    index index.php;

    location / {
        # URLs to attempt, including pretty ones
        try_files   $uri $uri/ /index.php?$query_string;
    }

    # TechnicSolder PHP FPM configuration.
    location /solder {
        alias  /var/www/solder.technicpack.com/htdocs/TechnicSolder/public;

        location ~ \.php$ {
             fastcgi_pass                    unix:/var/run/php5-fpm.sock;
             fastcgi_index                   index.php;
             fastcgi_split_path_info         ^(.+\.php)(.*)$;
             include                         fastcgi_params;
             fastcgi_param PATH_INFO         $fastcgi_path_info;
             fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        }
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
            deny all;
    }

    # Hide dot files/folders
    location ~ .*/\. {
            return 403;
    }

    # Set header expirations on per-project basis
    location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
            expires 365d;

    }
}
  • ok, so this is excatly what i have, i have the first main address working "mysamplesite.com" but when i try the "/solder" i get 404?

    server {
        listen 80; #IPv4
        listen [::]:80; #IPv6
    
        # Host that will serve this project.
        server_name     mysamplesite.com;
    
        # Useful logs for debugging.
        access_log      /home/solder/access.log;
        error_log       /home/solder/error.log;
        rewrite_log     on;
    
        # The location of our projects public directory.
        root            /usr/share/nginx/html;
        index index.php index.html;
    
        location /  {
            # URLs to attempt, including pretty ones
            try_files   $uri $uri/ /index.php?$query_string;
    
        }
    
        # PHP FPM configuration.
        location /solder {
            alias  /home/solder/public;
    
    
          location ~ \.php$ {
             try_files $uri =404;
             fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          }
        }
    
    
         # We don't need .ht files with nginx.
        location ~ /\.ht {
                deny all;
        }
    
        # Hide dot files/folders
        location ~ .*/\. {
                return 403;
        }
    
        # Set header expirations on per-project basis
        location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
                expires 365d;
    
        }
    }
    
    
    • Update. When i do "mysamplesite.com/home/solder" i get a 500 internal server error. the error log shows: rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 123.456.7.89 server: mysamplesite.com, request: "GET /home/solder HTTP/1.1", host: "mysamplesite.com"

Have another answer? Share your knowledge.