E
E
Egor Mikheev2015-11-19 12:39:46
Nginx
Egor Mikheev, 2015-11-19 12:39:46

How to setup 3rd level domains in Nginx + Apache2 with https?

Hello, I started setting up based on the article:
Level 3 dynamic domains


In general, the solution is simple - because. on the site, through mod_rewrite, the performance of links like www.example.com/users/([a-zA-Z_]+) has already been established, then it was decided to rewrite subdomains through nginx.
- in this case, I have no configurations with mod_rewrite for this part.
The result of the configs:
Not Found
The requested URL /basic/default/web/ was not found on this server.
Apache/2.4.10 (Debian) Server at default.privet.com Port 80

Nginx:
server {
    server_name mysite.com www.mysite.com;
    listen 80;
    return 301 https://mysite.com$request_uri;
}

server {
    server_name ~^(?<level>[a-z0-9\-]+)\.mysite.com$;
    listen 80;
    pagespeed off;

    location / {
      proxy_pass http://12.13.14.15:81/basic/$level/web$uri$is_args$args;
      proxy_redirect http://12.13.14.15:81/ /;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;					
    }
}



server {
    server_name mysite.com;
    listen 443 ssl;
    
    ssl_stapling on;
    ssl on;
    ssl_certificate /etc/nginx/sert/mysite.com.crt;
    ssl_certificate_key /etc/nginx/sert/mysite.com.key;
  #  ssl_dhparam /etc/pki/nginx/dhparam.pem;
    ssl_session_timeout 24h;
    ssl_session_cache shared:SSL:2m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers kEECDH+AES128:kEECDH:kEDH:-3DES:kRSA+AES128:kEDH+3DES:DES-CBC3-SHA:!RC4:!aNULL:!eNULL:!MD5:!EXPORT:!LOW:!SEED:!CAMELLIA:!IDEA:!PSK:!SRP:!SSLv2;
    ssl_prefer_server_ciphers on;
    add_header Strict-Transport-Security "max-age=31536000;";
    add_header Content-Security-Policy-Report-Only "default-src https:; script-src https: 'unsafe-eval' 'unsafe-inline'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /csp-report";
add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

    pagespeed off;
    pagespeed MapOriginDomain "http://localhost" "https://www.mysite.com";
                pagespeed LoadFromFile "https://mysite.me" "/www/mysite.com";

    access_log /var/log/nginx/access.log;
    set $root_path /www/mysite.com/;
  
    location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf)$ {
      root $root_path;
      error_page 404 = @fallback;
    }
    location / {
      proxy_pass http://12.13.14.15:81;
      proxy_redirect http://12.13.14.15:81/ /;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;
    }
    
    location @fallback {
      proxy_pass http://12.13.14.15:81;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;
    }
  }

Apache2:
<Directory /www/mysite.com>
     AllowOverride All
</Directory>

<VirtualHost 12.13.14.15:81 >
        ServerName mysite.com
        DocumentRoot /www/mysite.com/
        ServerAdmin [email protected]
        ServerAlias www.mysite.com
</VirtualHost>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question