這是本文件的舊版!


CentOS 安裝 Nginx 擔任網站分派器

rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
chkconfig nginx on
  • 假設連上 nginx 有 abc.ichiayo.com 與 xyz.ichiayi.com 兩個 Virtual Host 分別對應到 192.168.11.233 / 192.168.11.232
vi /etc/nginx/nginx.conf
:
worker_processes  2;
:
    gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
:
vi /etc/nginx/conf.d/abc_ichiayi.conf
server {
        server_name     abc.ichiayi.com abc1.ichiayi.com;

        access_log /var/log/nginx/abc.ichiayi.com.access.log main;
        error_log /var/log/nginx/abc.ichiayi.com.error.log;

        location / {
                set_real_ip_from  192.168.11.0/24;
                real_ip_header    X-Forwarded-For;
                real_ip_recursive on;

                proxy_pass       http://192.168.11.233:80;
                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        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /292/ {
                proxy_pass http://192.168.11.292/;
        }
}
vi /etc/nginx/conf.d/xyz_ichiayi.conf
server {
        server_name     xyz.ichiayi.com;

        access_log /var/log/nginx/xyz.ichiayi.com.access.log main;
        error_log /var/log/nginx/xyz.ichiayi.com.error.log;

        location / {
                set_real_ip_from  192.168.11.0/24;
                real_ip_header    X-Forwarded-For;
                real_ip_recursive on;

                proxy_pass       http://192.168.11.232:80;
                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        X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}
  • 當設定多個 Virtual Host 之後, 如果 DNS 有設定, 但未設定 Virtual Host 的 Domain Name 希望能指定預設網站, 可在預設的 server 設定檔內設定如下:
    server {
        listen      192.168.1.2:80 default_server;
        server_name example.com www.example.com;
        ...
    }
  • 也就是在 listen 80 後面加上 default_server
  • https 也可以這樣指定 Exp.
    :
    listen 443 ssl default_server;
    :
  • tech/nginx.1569317591.txt.gz
  • 上一次變更: 2019/09/24 17:33
  • jonathan_tsai