I
I
iManiacDev2012-09-30 17:48:47
Nginx
iManiacDev, 2012-09-30 17:48:47

Nginx and two sites on the same domain

There are 2 sites on the same domain, the whole thing is managed by clean fresh nginx. Depending on the value of the $var variable, I need to issue different versions of the site. The sites are in one common directory:

Directory parent \
Directory parent \ Site1
Directory parent \ Site2

$var takes the values ​​Site1 and Site2.

For one site, everything is clear, the nginx config takes the following form:

server {
  listen 80;
  server_name domain.com;
  
  root /var/www/Общая директория; 
  index index.php index.html;

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


  location ~ /index.php {
      include fastcgi_params;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
       
  location ~* \.(php|tpl|xml|log)$ { return 403; }
} 


How to remake this config to solve this problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2012-10-04
@iManiacDev

I give a universal config for a bunch of domains proxied through nginx with an automatic redirect of domains from www to without www. Server on Ubuntu.
nginx.conf

user www-data;
worker_processes 1;
pid /var/run/nginx.pid;

events {
  worker_connections 4096;
  multi_accept on;
}

http {


  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  client_max_body_size 100M;
  types_hash_max_size 2048;

  server_names_hash_bucket_size 64;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;


  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;


  gzip on;
  gzip_disable "msie6";

  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 5;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  map $host $host_wo_www {
    default $host;
    ~^www\.(?P<wo_www>.+)$ $wo_www;
  }
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}


In sites-enabled/default
server {
  listen 80;
  server_name ~^(?:www\.)?(?P<host_wo_www>.+)$;
  server_name_in_redirect	off;
  resolver	127.0.0.1;

  access_log	/var/log/httpd/$host/access.log;
  location	~* \.(?:jpe?g|gif|png|ico|css|zip|tgz|gz|rar|bz2|dic|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf|flv|mp3)$
  {
    root	/var/www/$host_wo_www/www;
    access_log off;
    expires	30d;
    add_header Pragma public;
    add_header Cache-Control public;
  }

  location / {
    root	/var/www/$host_wo_www/www;
    index	index.html index.htm index.php;
    access_log	off;
    proxy_pass http://127.0.0.1:8081;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

A
AGvin, 2012-09-30
@AGvin

In general, in " location / " You can register a couple more locations

server {
    listen   80;
    server_name domain.com;

    location / {
  root   /var/www/path/to/;
  ...
    	location /project1/ {
      root /var/www/path/to/project1/;
      ...
  }
    	location /project2/ {
      root /var/www/path/to/project2/;
      ...
  }
    }
}

Where "..." are general parameters (the same "include fastcgi_params;" or "fastcgi_pass unix:/var/run/php5-fpm.sock;"). You can put them in another config and connect them using "include"

A
Andrey Burov, 2012-09-30
@BuriK666

add

root /var/www/$var;

Where do you get $var from?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question