差異處
這裏顯示兩個版本的差異處。
兩邊的前次修訂版 前次修改 下次修改 | 前次修改 | ||
tech:nginx [2019/04/19 13:49] – jonathan_tsai | tech:nginx [2021/03/17 14:14] (目前版本) – [設定 Virtual Host] jonathan | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== CentOS 安裝 Nginx 擔任網站分派器 ====== | ||
+ | ===== 安裝 nginx yum repo ===== | ||
+ | ==== CentOS 6 ==== | ||
+ | <code sh> | ||
+ | rpm -ivh http:// | ||
+ | </ | ||
+ | ==== CentOS 7 ==== | ||
+ | <code sh> | ||
+ | rpm -ivh http:// | ||
+ | </ | ||
+ | |||
+ | ===== 安裝 nginx web-server ===== | ||
+ | <code sh> | ||
+ | yum install nginx | ||
+ | </ | ||
+ | |||
+ | ===== 設定開機自動啟動 nginx ===== | ||
+ | <code sh> | ||
+ | chkconfig nginx on | ||
+ | </ | ||
+ | |||
+ | ===== 設定 Virtual Host ===== | ||
+ | * 假設連上 nginx 有 abc.ichiayo.com | ||
+ | <code sh> | ||
+ | vi / | ||
+ | </ | ||
+ | : | ||
+ | worker_processes | ||
+ | : | ||
+ | gzip on; | ||
+ | |||
+ | include / | ||
+ | } | ||
+ | </ | ||
+ | vi / | ||
+ | </ | ||
+ | server { | ||
+ | listen | ||
+ | server_name | ||
+ | : | ||
+ | </ | ||
+ | vi / | ||
+ | </ | ||
+ | server { | ||
+ | server_name | ||
+ | |||
+ | access_log / | ||
+ | error_log / | ||
+ | |||
+ | location / { | ||
+ | set_real_ip_from | ||
+ | real_ip_header | ||
+ | real_ip_recursive on; | ||
+ | |||
+ | proxy_pass | ||
+ | proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; | ||
+ | proxy_redirect off; | ||
+ | proxy_buffering off; | ||
+ | proxy_set_header Host $host; | ||
+ | proxy_set_header X-Real-IP $remote_addr; | ||
+ | proxy_set_header | ||
+ | } | ||
+ | |||
+ | location /292/ { | ||
+ | proxy_pass http:// | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | vi / | ||
+ | </ | ||
+ | server { | ||
+ | server_name | ||
+ | |||
+ | access_log / | ||
+ | error_log / | ||
+ | |||
+ | location / { | ||
+ | set_real_ip_from | ||
+ | real_ip_header | ||
+ | real_ip_recursive on; | ||
+ | |||
+ | proxy_pass | ||
+ | proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; | ||
+ | proxy_redirect off; | ||
+ | proxy_buffering off; | ||
+ | proxy_set_header Host $host; | ||
+ | proxy_set_header X-Real-IP $remote_addr; | ||
+ | proxy_set_header | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | * 當設定多個 Virtual Host 之後, 如果 DNS 有設定, 但未設定 Virtual Host 的 Domain Name 希望能指定預設網站, | ||
+ | server { | ||
+ | listen | ||
+ | server_name example.com www.example.com; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | * 也就是在 listen 80 後面加上 **default_server** | ||
+ | * https 也可以這樣指定 Exp. < | ||
+ | : | ||
+ | listen 443 ssl default_server; | ||
+ | : | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | <WRAP center round info 80%> | ||
+ | * 因有朋友告知 http:// | ||
+ | server { | ||
+ | server_name | ||
+ | : | ||
+ | : | ||
+ | ssl_dhparam / | ||
+ | |||
+ | } | ||
+ | |||
+ | server { | ||
+ | if ($host = www.ichiayi.com) { | ||
+ | return 301 https:// | ||
+ | } # managed by Certbot | ||
+ | |||
+ | if ($host = ichiayi.com) { | ||
+ | return 301 https:// | ||
+ | } | ||
+ | |||
+ | listen | ||
+ | server_name | ||
+ | return 404; # managed by Certbot | ||
+ | } | ||
+ | </ | ||
+ | systemctl restart nginx | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== 其他相關資訊 ===== | ||
+ | * **[[tech/ | ||
+ | * **[[tech/ | ||
+ | |||
+ | ===== 參考網址 ===== | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * https:// | ||
+ | |||
+ | {{tag> |