A
A
Alexander2019-06-08 18:48:36
Nginx
Alexander, 2019-06-08 18:48:36

How to set up NGINX to have different sites in different folders?

There is a folder in which the site is located
/var/www/dev.example.com
Inside the site folder there are two folders - forum and public
The following config is used

View config
server {
  listen 80;
  #listen [::]:80;

  listen 443 ssl http2;
  #listen [::]:443 ssl http2;

  ssl_certificate /etc/letsencrypt/live/dev.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/dev.example.com/privkey.pem;

  if ($scheme = http) {
    return 301 https://$server_name$request_uri;
  }

  server_name dev.example.com;

  charset utf-8;
  access_log /var/log/nginx/dev.example.com.access.log;
  error_log /var/log/nginx/dev.example.com.error.log;

  root /var/www/dev.example.com/public;
  
  index index.html index.htm index.php;

  # Dev-хост, отключение кеширования
  add_header Last-Modified $date_gmt;
  add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
  if_modified_since off;
  expires off;
  etag off;

  location / {
    try_files $uri $uri/ /index.php$is_args$args;
  }
  
  # Не удалять! Нужно для обновления сертификатов от Let's Encrypt
  location ~ /.well-known/acme-challenge {
    allow all;
  }

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

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

Now everything is configured so that when requesting a site, NGINX looks for all files in the "/var/www/dev.example.com/public" folder. But I want to make it so that when requesting dev.example.com/forum, NGINX looks for files in "/var/www/dev.example.com/forum" and not "/var/www/dev.example.com" /public /forum". And of course, to also process PHP scripts
. How can I do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-06-08
@dodo512

For forum, add a separate location.

location /forum {
    root /var/www/dev.example.com;
    
    try_files $uri $uri/ /forum/index.php$is_args$args;
    
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question