(實(shí)戰(zhàn))nginx反向代理及重定向部署

  • 一、安裝的步驟,參考 https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7

    簡(jiǎn)單而言,步驟如下所示:

    yum install epel-release
    
    yum install nginx
    
    systemctl start nginx
    

    這樣就成了,訪問(wèn) http://<server_domain_name_or_IP>/ 即可看到Welcome頁(yè)面。

    systemctl enable nginx
    
  • 二、要讓php跟nginx搭配使用,下面是安裝php的步驟

    yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
    
    yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64
    
    yum-config-manager --enable remi-php72
    

    如果報(bào)沒(méi)找到 yum-config-manager 命令,那么先執(zhí)行 yum install yum-utils 安裝

    yum install php php-mysqlnd php-fpm
    
    php --version
    
    vim /etc/php-fpm.d/www.conf
    
    user = nginx
    group = nginx
    listen = /var/run/php-fpm/php-fpm.sock
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0660
    
    systemctl start php-fpm
    
    systemctl enable php-fpm
    
    vim /etc/nginx/conf.d/default.conf
    
    server {
        listen       80;
        server_name  server_domain_or_IP;
    
        root   /usr/share/nginx/html;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
    
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    
    systemctl restart nginx
    
  • 三、設(shè)置反向代理

    • 理解基本知識(shí)參考:https://www.cnblogs.com/ysocean/p/9392908.html

    • 在/etc/nginx/conf.d里增加自定義的配置文件,配置示例:

      server {
          listen       80;
          server_name aaa.jinglever.com bbb.jinglever.com;
          location / {
              proxy_pass $scheme://$host$request_uri;
          }
          resolver 8.8.8.8;
      
          access_log           /var/log/nginx/access_log;
          error_log            /var/log/nginx/error_log warn;
      }
      
      server {
          listen       443 ssl;
          ssl_certificate         /etc/letsencrypt/live/jinglever.com/fullchain.pem;
          ssl_certificate_key     /etc/letsencrypt/live/jinglever.com/privkey.pem;
      
          server_name aaa.jinglever.com bbb.jinglever.com;
          location / {
              proxy_pass $scheme://$host$request_uri;
          }
          resolver 8.8.8.8;
      
          access_log           /var/log/nginx/access_log;
          error_log            /var/log/nginx/error_log warn;
      }
      
    • 幾個(gè)細(xì)節(jié)的說(shuō)明:

      • server_name 支持把多個(gè)域名寫(xiě)到一起
      • $host 是當(dāng)前訪問(wèn)的域名,注意這里不能用 $server_name
      • $request_uri 是當(dāng)前訪問(wèn)的uri
      • 一定要有 resolver,不然會(huì)報(bào)錯(cuò)
      • 上面實(shí)現(xiàn)的效果舉例:我在本地修改host,把a(bǔ)aa.jinglever.com解析到這臺(tái)反向代理服務(wù)器的ip上,在我本地訪問(wèn)aaa.jinglever.com,會(huì)通過(guò)反向代理服務(wù)器最終訪問(wèn)到真正的aaa.jinglever.com服務(wù)器。
    • 注意事項(xiàng):

      • 必須有個(gè)兜底的server,即當(dāng)沒(méi)匹配上前面的server_name時(shí),有個(gè)默認(rèn)的server對(duì)請(qǐng)求進(jìn)行處理。否則,nginx會(huì)把請(qǐng)求丟給上面的server,即便server_name不匹配。試想,按照前面的配置,且沒(méi)有兜底的server,如果在瀏覽器上直接通過(guò)ip訪問(wèn)進(jìn)來(lái),會(huì)有什么結(jié)果呢?答案就是:會(huì)無(wú)限循環(huán)代理訪問(wèn)直到系統(tǒng)文件打開(kāi)數(shù)超限。
      • 如果代理了ssl訪問(wèn),別忘了兜底ssl訪問(wèn)的server哦。
  • 四、設(shè)置重定向

    • 在/etc/nginx/conf.d里增加自定義的配置文件,配置示例:

      server {
          listen       80;
          server_name ~(.*)\.jinglever\.cn$ ~(.*)\.jinglever\.net$;
          set $subdomain $1 ;
          rewrite ^/(.*)$ $scheme://$subdomain.howard.com/$1 permanent;
      
          access_log           /var/log/nginx/access_log;
          error_log            /var/log/nginx/error_log warn;
      }
      
      server {
          listen       443 ssl;
          ssl_certificate         /etc/letsencrypt/live/jinglever.cn/fullchain.pem;
          ssl_certificate_key     /etc/letsencrypt/live/jinglever.cn/privkey.pem;
      
          server_name ~(.*)\.mikesent\.cn$;
          set $subdomain $1 ;
          rewrite ^/(.*)$ $scheme://$subdomain.howard.com/$1 permanent;
      
          access_log           /var/log/nginx/ssl_request_log;
          error_log            /var/log/nginx/ssl_error_log warn;
      }
      
    • 幾個(gè)細(xì)節(jié)的說(shuō)明:

      • 將訪問(wèn)的域名里的子域名部分提取出來(lái),賦給 $subdomain。
      • rewrite那行最后的 permanent,使得重定向code為301,表示永久重定向。如果要表示臨時(shí)重定向,就用 redirect 。
      • 實(shí)現(xiàn)的效果舉例:我訪問(wèn) aaa.bbb.jinglever.cn/ccc/ddd,nginx會(huì)把訪問(wèn)重定向到 aaa.bbb.howard.com/ccc/ddd。
  • 五、好用的工具網(wǎng)站

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容