使用Nginx的多个域(反向代理)

时间:2018-05-22 08:56:45

标签: nginx dns admin reverse-proxy

我使用以下配置在我的debian服务器上启动并运行nginx:

root@serverAUS:/var/log/nginx# nginx -V
nginx version: nginx/1.10.3
built with OpenSSL 1.1.0f  25 May 2017
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-2tpxfc/nginx-1.10.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-dynamic-module=/build/nginx-2tpxfc/nginx-1.10.3/debian/modules/nginx-auth-pam --add-dynamic-module=/build/nginx-2tpxfc/nginx-1.10.3/debian/modules/nginx-dav-ext-module --add-dynamic-module=/build/nginx-2tpxfc/nginx-1.10.3/debian/modules/nginx-echo --add-dynamic-module=/build/nginx-2tpxfc/nginx-1.10.3/debian/modules/nginx-upstream-fair --add-dynamic-module=/build/nginx-2tpxfc/nginx-1.10.3/debian/modules/ngx_http_substitutions_filter_module

我的nginx配置看起来像是:

server {
    listen 80;
    # first domain
    server_name firstsubdomain.domain.tld;

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

    location / {
        proxy_pass http://192.168.XX.YY;
    }
}

我想知道我们是否可以为指定的虚拟主机设置另一个子域。

server {
    listen 80;

    # first domain
    server_name firstsubdomain.domain.tld;

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

    # another subodmain
    servername secondsubdomain.domain.tld;

    location / {
        proxy_pass http://192.168.XX.YY;
    }
}

它没有用,我不明白。我试图更改我的Mac地址和我的IP地址,但它仍然没有工作......

root@serverAUS:~# service nginx restart
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.

Gabriel Tretyakova

2 个答案:

答案 0 :(得分:0)

将两个服务器名称放在同一个块上,确保server_name具有下划线:

$ ls -l
  588890 output
 3288710 output.gz

答案 1 :(得分:0)

您可以检查以下配置。这里,三个子域 www admin api 托管在同一台服务器上(DigitalOcean - Ubuntu)。所有三个子域都指向不同的目录。

# http://www.example.com
server {
    charset utf-8;
    client_max_body_size 128M;
    sendfile off;
    server_name www.example.com;
    root /var/www/html/example/frontend/web;
    index index.php;
    access_log /var/log/nginx/www.example.com-access.log;
    error_log /var/log/nginx/www.example.com-error.log;
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        try_files $uri =404;
    }
    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

# http://admin.example.com
server {
    charset utf-8;
    client_max_body_size 128M;
    sendfile off;
    server_name admin.example.com;
    root /var/www/html/example/backend/web;
    index index.php;
    access_log /var/log/nginx/admin.example.com-access.log;
    error_log /var/log/nginx/admin.example.com-error.log;
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        try_files $uri =404;
    }
    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

# http://api.example.com
server {
    charset utf-8;
    client_max_body_size 128M;
    sendfile off;
    server_name api.example.com;
    root /var/www/html/example/api/web;
    index index.php;
    access_log /var/log/nginx/api.example.com-access.log;
    error_log /var/log/nginx/api.example.com-error.log;
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        try_files $uri =404;
    }
    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

# http://api.example.com
server {
    charset utf-8;
    client_max_body_size 128M;
    sendfile off;
    server_name cdn.example.com;
    root /var/www/html/cdn;
    index index.php;
    access_log /var/log/nginx/cdn.example.com-access.log;
    error_log /var/log/nginx/cdn.example.com-error.log;
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        try_files $uri =404;
    }
    location ~ /\.(ht|svn|git) {
        deny all;
    }
}