首页 > 代码库 > 【Linux】nginx服务配置

【Linux】nginx服务配置

一. 部署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服务配置