Welcome! Log In Create A New Profile

Advanced

Nginx proxy for wildcard domain

Posted by rocklee44 
Nginx proxy for wildcard domain
May 06, 2016 07:54AM
Hi all,
Here my case:
I have wildcard certificate for *.domain.com .
What I want :
- http requests to domain.com or *.domain.com are redirect to https
- https requests to domain.com are passed to directory homesite port 82
- https request to management.domain.com are passed to directory management port 82
- https request to site1.domain.com , site2.domain.com , anythings.domain.com , ... (*.domain.com) are passed to directory wildcard port 82
How should I configure Nginx ? Please give me some advice.
Thank you very much.
Re: Nginx proxy for wildcard domain
May 06, 2016 10:27PM
I'm using Nginx-1.8.1-1.el6.ngx.x86_64 on Centos 6.4 64 bit, here what I tried :

user nginx;
worker_processes 8;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

log_format proxy '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $upstream_response_time';

access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;

server {
listen 192.168.201.11:82 default;
server_name _;

access_log /var/log/nginx/access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 192.168.201.11:80 default_server;
server_name *.domain.com;
return 301 https://$server_name$request_uri;
}

server {
listen 192.168.201.11:443 ssl;
ssl_certificate /opt/certificate/cert.crt;
ssl_certificate_key /opt/certificate/cert.key;
server_name *.domain.com;

access_log /var/log/nginx/proxy-access.log proxy;
error_log /var/log/nginx/proxy-error.log;
include proxy_params;
add_header X-Proxy-Cache $upstream_cache_status;

location / {
proxy_pass http://http;
}
}

upstream http {
ip_hash;
server 192.168.201.11:82 weight=1 max_fails=3 fail_timeout=30s;
}

server {
listen 192.168.201.11:82;
server_name domain.com;
access_log /var/log/nginx/domain/domain-access.log proxy;
error_log /var/log/nginx/domain/domain-error.log;
root /opt/www/domain/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.html index.php;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /opt/www/domain$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

server {
listen 192.168.201.11:82;
server_name management.domain.com;
access_log /var/log/nginx/management/management-access.log proxy;
error_log /var/log/nginx/management/management-error.log;
root /opt/www/management/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.html index.php;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /opt/www/management$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

server {
listen 192.168.201.11:82;
server_name *.domain.com;
access_log /var/log/nginx/wildcard-domain/wildcard-domain-access.log proxy;
error_log /var/log/nginx/wildcard-domain/wildcard-domain-error.log;
root /opt/www/wildcard-domain/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.html index.php;
}

location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /opt/www/wildcard-domain$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

But it doesn't work.
http://domain.com redirect to https://domain.com
http://management.domain.com or something.domain.com redirect to https://%2A.domain.com/
Re: Nginx proxy for wildcard domain
May 06, 2016 10:54PM
it works fine if I change return 301 https://$server_name$request_uri; ==> return 301 https://$host$request_uri;
My proxy_params :
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 250
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready