1. 程式人生 > >【Linux】nginx服務配置

【Linux】nginx服務配置

網站目錄 write timeout per dex 修改 header www val

一. 部署LNMP環境

    準備工作   Linux系統準備   
              設置IP   
              關閉防火墻
              yum源配置

    安裝: 傳輸軟件包
    1. tar -zxvf  lnmp1.2-full.tar.gz 
       cd  lnmp1.2-full
       ./install.sh  lnmp 

二. 實驗1 虛擬主機

    www.sina.com   www.sohu.com

    1.域名解析  (文件解析)
    2.規劃網站目錄 
      mkdir /home/wwwroot/sina/
      mkdir /home/wwwroot/sohu/
      vim /home/wwwroot/sina/index.html
      vim /home/wwwroot/sohu/index.html

    3.修改配置文件
    vim /usr/local/nginx/conf/nginx.conf
     66         listen 80;

    4.建立虛擬主機文件 v.conf
    vim /usr/local/nginx/conf/vhost/v.conf
      1 server {
      2         listen 80;
      3         server_name www.sina.com;
      4         index index.html index.htm index.php;
      5         root /home/wwwroot/sina;
      6 
      7         include enable-php.conf;
      8 
      9 }
     10 
     11 server {
     12         listen 80;
     13         server_name www.sohu.com;
     14         index index.html index.htm index.php;
     15         root /home/wwwroot/sohu;
     16         
     17         include enable-php.conf;
     18 
     19 }

    5.重啟服務  測試
    pkill -HUP nginx  

    測試 www.sina.com   www.sohu.com


    實驗2 rewrite 重寫/重定向

    域名跳轉  www.sina.com  -> www.sohu.com

    vim /usr/local/nginx/conf/vhost/v.conf
      1 server {
      2         listen 80;
      3         server_name www.sina.com;
      4         index index.html index.htm index.php;
      5         root /home/wwwroot/sina;
      6 
      7         include enable-php.conf;
      8         location /nginx_status{
      9         stub_status on;
      10         access_log  off;
      11         }
      12         if ($http_host = www.sina.com) {
      13                         rewrite  (.*)  http://www.sohu.com  permanent;
      14                 }
      15 }

    重啟服務
        pkill -HUP nginx

    測試 
        www.sina.com  -> www.sohu.com



    網頁文件跳轉
    1.修改配置文件
    vim /usr/local/nginx/conf/vhost/v.conf
      1 server {
      2         listen 80;
      3         server_name www.sina.com;
      4         index index.html index.htm index.php;
      5         root /home/wwwroot/sina;
      6 
      7         include enable-php.conf;
      8         location /nginx_status{
      9         stub_status on;
      10         access_log  off;
      11         }
      12         
      13         rewrite  index(\d+).html   /index.php?id=$1   last;
      14  }

     2.建立index.php 文件
     vim  /home/wwwroot/sina/index.php
     <?php  echo "Sina rewrite!" ?>

     3.重啟服務  測試
     pkill -HUP nginx

     測試  www.sina.com/index3.html


    實驗3 代理負載均衡 (反向代理)

       準備: Nginx S    192.168.183.251     
             Apache S1  192.168.183.123
             Apache S2  192.168.183.103

       搭建步驟1.修改S Nginx 配置文件
               vim /usr/local/nginx/conf/nginx.conf
                66 upstream myweb1 {
                67         server 192.168.183.123:80;
                68         server 192.168.183.103:80;
                69 }       
                70 server {
                71                listen       80;
                72                 server_name  www.sohu.com;
                73         location / {
                74                 proxy_pass http://myweb1;
                75                 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
                76                 proxy_set_header Host $host;
                77                 proxy_set_header X-Forwarded-For $remote_addr;
                78 }
                79 }

            2.配置S1 Apache 192.168.183.123     正常訪問
             登錄到S1    關閉autoindex    vhosts 功能
             vim /usr/local/apache2/htdocs/index.html
             S1111111111111

             測試  192.168.183.123 

             3.配置S2 Apache 192.168.183.103     正常訪問
             登錄到S1    關閉autoindex    vhosts 功能
             vim /usr/local/apache2/htdocs/index.html
             S22222222222222

             測試  192.168.183.103 

             4.重啟S Nginx服務  測試
             pkill -HUP nginx

             測試 www.sohu.com 

【Linux】nginx服務配置