【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 
时间: 2024-08-25 05:57:53

【Linux】nginx服务配置的相关文章

Kali Linux常用服务配置教程安装及配置DHCP服务

Kali Linux常用服务配置教程安装及配置DHCP服务 在Kali Linux中,默认没有安装DHCP服务.下面将介绍安装并配置DHCP服务的方法. 1.安装DHCP服务 在Kali Linux中,用来提供DHCP服务的安装包名为isc-dhcp-server.所以,执行命令如下所示: [email protected]:~# apt-get install isc-dhcp-server –y 执行以上命令后,将开始安装DHCP服务.如果安装过程中没有报错的话,则该服务将被成功安装到系统中

源码编译Nginx服务配置

一.实验环境: RHEL7.0 172.25.254.1 server1.example.com  firewalld disable 二.实验内容:     1.源码安装Nginx nginx-1.9.14.tar.gz        下载源码包 tar zxf nginx-1.9.14.tar.gz         cd nginx-1.9.14/         vim auto/cc/gcc # debug        #CFLAGS="$CFLAGS -g"    #关闭d

nginx服务配置---php服务接入

前言: 最近要搭建一个内部的wiki系统, 网上搜了一圈, 也从知乎上搜集了一些大神的评价和推荐. 重点找了几个开源的wiki系统, 不过发现他们都是采用php来实现的. 于是乎需要配置php环境, 来配合服务正常工作. 网上多是apache+php的组合方式, 不过由于个人是nginx脑残粉, 因此决定采用nginx+php fastcgi来配置下环境. 思路梳理: 云主机是ubuntu系统(主要觉得apt好用, 当然centos的yum也是利器). 对于php, php-fpm, 以及ngi

Linux Nginx 安装配置

安装前准备工作 GCC编译器-程序代码编译工具! 首先检验你的服务器环境是否安装gcc,方法如下: #gcc 如果出现:gcc: no input files 证明已经安装过gcc编译工具! 如果出现:gcc: command not found 说明还没有安装过gcc编译工具! # yum install gcc 编译Nginx时需要用到PCRE,同时Nginx的Rewrite和http模块也要用到PCRE的语法!需要安装pcre包pcre-devel包.pcre包负责提供库的编译版本,pcr

Nginx服务配置综合实例

################################ 1.安装nginx,yum安装,编译安装 Nginx是一个免费,开源,高性能的HTTP服务器,同时也可以作为反向代理服务器,支持IMAP/POP3邮件代理服务器,支持模块化定制功能. Nginx支持三种运行模式,默认为worker模式: prefork:进程模型,两级结构,主进程master负责生成和管理子进程,每个子进程负责响应一个请求: worker:线程模型,三级结构,主进程负责生成子进程,每个子进程负责生成多个线程,每个线

Linux vsftpd服务配置详解

[背景] 近日,一朋友domino服务器要进行升级.迁移,搭建了linux测试系统,也开启vsftpd服务,可是配置的ftp账号,程序无法正常下载附件. [问题跟踪] 通过ftp客户端连接工具登录,发现未跳转到ftp账号指定的目录下面. cat /etc/vsftpd.config发现开启了: local_root=/home/test 将此注释重启ftp服务正常. 附录(vsftpd配置详解): vsftpd服务器配置文件"/etc/vsftpd/vsftpd.conf",以此为例.

Nginx服务配置(一)

1.环境准备 [[email protected] ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo //更换YUM源,centos默认源是国外的网站,下载会比较慢 [[email protected] nginx-1.6.3]# yum -y install gcc*   //安装编译环境 2.安装pcre库,如果不安装pcre库,Nginx无法使用rewrite

编写nginx服务配置

三个语法格式说明:①. 大括号要成对出现②. 每一行指令后面要用分号结尾③. 每一个指令要放置在指定的区块中(一)实现编写一个网站页面worker_processes 1;events {worker_connections 1024;}http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name www.e

linux启动服务配置详解

init 进程是所有进程的发起者和控制者.因为在任何基于 Unix 的系统(比如 linux)中,它都是第一个 运行的进程,所以 init 进程的编号(Process ID,PID)永远是 1.如果 init 出现了问题,系统的其余部分 也就随之而垮掉了. init 服务 init 进程是所有进程的发起者和控制者.因为在任何基于 Unix 的系统(比如 linux)中,它都是第一个 运行的进程,所以 init 进程的编号(Process ID,PID)永远是 1.如果 init 出现了问题,系统