iT邦幫忙

0

請教 nginx 轉子網域導入 apache 設定?

因有一個 web 服務要設多語系,是用 php 寫的.
用子網域做為切換多語系功能.

所以 .env 定義:
LANG_EN_URL=en.webtest.aidec.com.tw
LANG_TW_URL=tw.webtest.aidec.com.tw
LANG_CN_URL=cn.webtest.aidec.com.tw

這三個都指到
/var/www/html/web/public 資料夾下

Apache 是內網主機: 10.10.10.133

vi /etc/httpd/conf/httpd.conf

++++++++++++++++++++++++++++++++++++++++++++++++++
...
IncludeOptional conf.d/.conf
IncludeOptional sites-enabled/
.conf # 加入此行
++++++++++++++++++++++++++++++++++++++++++++++++++

mkdir /etc/httpd/sites-enabled/

vi /etc/httpd/sites-enabled/subdomain.conf

++++++++++++++++++++++++++++++++++++++++++++++++++

主網域的配置

<VirtualHost *:80>
ServerAdmin service@aidec.com.tw
ServerName webtest.aidec.com.tw
# ServerAlias aidec.tw
DocumentRoot /var/www/html/web
ErrorLog /var/www/html/error.log
CustomLog /var/www/html/requests.log combined

子網域的配置

<VirtualHost *:80>
DocumentRoot /var/www/html/web/public
ServerName en.webtest.aidec.com.tw

<VirtualHost *:80>
DocumentRoot /var/www/html/web/public
ServerName tw.webtest.aidec.com.tw

<VirtualHost *:80>
DocumentRoot /var/www/html/web/public
ServerName cn.webtest.aidec.com.tw

++++++++++++++++++++++++++++++++++++++++++++++++++

systemctl restart httpd.service

上述不透過 nginx 會成功!

但若改為 nginx 80 轉為 443 在對應至 apache 子網域,會失敗?
請問 nginx 要如何設定?

我設定 nginx 如下:
nginx\conf.d\tw.webtest.aidec.com.tw
server {
listen 80;
server_name tw.aidec.tej.com.tw;
return 301 https://tw.webtest.aidec.com.tw$request_uri;
server_tokens off;
}

server {
listen 443 ssl;
server_name tw.webtest.aidec.com.tw; #change to your domain name

    ssl on;

    ssl_certificate /etc/nginx/aidec_ssl/domaincert.cer;
    ssl_certificate_key /etc/nginx/aidec_ssl/server.key;

    ssl_dhparam /etc/nginx/dhparam.pem;
    ssl_session_cache shared:le_nginx_SSL:1m;
    ssl_session_timeout 1440m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256 ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-ECDSA-AES128-SHA ECDHE-ECDSA-AES256-SHA ECDHE-ECDSA-AES128-SHA256 ECDHE-ECDSA-AES256-SHA384 ECDHE-RSA-AES128-GCM-SHA256 ECDHE-RSA-AES256-GCM-SHA384 ECDHE-RSA-AES128-SHA ECDHE-RSA-AES128-SHA256 ECDHE-RSA-AES256-SHA384 DHE-RSA-AES128-GCM-SHA256 DHE-RSA-AES256-GCM-SHA384 DHE-RSA-AES128-SHA DHE-RSA-AES256-SHA DHE-RSA-AES128-SHA256 DHE-RSA-AES256-SHA256 EDH-RSA-DES-CBC3-SHA";

    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security "max-age=31536000; preload";

    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_pass http://10.10.10.133/web/public;       #change to your internal server IP
            proxy_redirect off;
    }

}

敬請諸位大大幫忙!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
㊣浩瀚星空㊣
iT邦大神 1 級 ‧ 2020-09-17 10:06:48
最佳解答

你在nginx是用proxy轉向的,但你用ip轉向。
但你在 Apache 是設定子網域。理論上用ip指向會無法對應到的。
基本可以教你幾招。
第一種是用port招。

<VirtualHost *:8081>
DocumentRoot /var/www/html/web/public
ServerName en.webtest.aidec.com.tw
<VirtualHost *:8082>
DocumentRoot /var/www/html/web/public
ServerName tw.webtest.aidec.com.tw
<VirtualHost *:8083>
DocumentRoot /var/www/html/web/public
ServerName cn.webtest.aidec.com.tw

這樣就可以用指定的port去做對應。

第二招是host招,只是這招有危險性
去改你的host文件。將對應的域名ip設定。
然後直接proxy域名不要用ip。

但一般來說,建議還是不要跟原域名一樣的。

最後,再教你一招,http轉https的nginx寫法。
改成底下這樣子,以後只要動server_name就好。

server {
    listen       80;
    server_name  abc.com;      
    return 301 https://$host$request_uri;
}
kuang001 iT邦新手 3 級 ‧ 2020-09-17 11:46:50 檢舉

大大: 謝謝教導.我在實做看看.

我要發表回答

立即登入回答