簡體   English   中英

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

[英]Multiple domains with Nginx (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)

將兩個服務器名稱放在同一塊上,確保server_name下划線:

server_name
firstsubdomain.domain.tld
secondsubdomain.domain.tld;

您可以檢查以下配置。 在這里,三個子域wwwadminapi托管在同一服務器(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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM