2021-10-16

Nginx 設定多台 web server

Nginx 和 Apache 一樣都可以設定多台 virtual hosts,服務多個站台。在此以 shinder.cc 和 www.shinder.cc 為例說明。系統為 Debian 10,預先安裝 Nginx,PHP 和 certbot 的方式請參考之前的 在 Debian 上安裝 NginX, PHP-FPM 環境和 Certbot。 以下有三個設定檔,皆位於 /etc/nginx/sites-enabled,其中 default-http 為處理 http 使其轉向到 https,另外兩個則是分別使用不同的後端技術,做為 web server 的服務功能。

# default-http server { if ($host = www.shinder.cc) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = shinder.cc) { return 301 https://$host$request_uri; } # managed by Certbot listen 80 ; listen [::]:80 ; server_name www.shinder.cc shinder.cc; return 404; # managed by Certbot }
# shinder.cc 使用 NodeJS server { server_name shinder.cc; listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/shinder.cc/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/shinder.cc/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
# www.shinder.cc 使用 PHP server { server_name www.shinder.cc; listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/shinder.cc/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/shinder.cc/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.3-fpm.sock; } }

沒有留言:

FB 留言