Nginx配置实例

Nginx服务器基础配置实例

1、配置实例

前面我们已经对Nginx服务器默认配置文件结构和涉及的基本指令做了详细的阐述。通过这些指令的合理配置,我们就可以让一台Nginx服务器正常工作,并且提供基本的Web服务器功能。
接下来我们将通过一个比较完整和最简单的基础配置实例,来巩固下面所学习的指令及其配置。
需求如下:

(1) 有如下访问:
http://192.168.24.62:8081/server1/location1
访问的是: index_sr1_location1.html
http://192.168.24.62:8081/server1/location2
访问的是: index_sr1_location1.html
http://192.168.24.62:8082/server2/location1
访问的是: index_sr2_location1.html
http://192.168.24.62:8082/server2/location2
访问的是: index_sr2_location2.html
(2)如果访问的资源不存在,返回自定义的404页面
(3)将/server1 和 /server2的配置使用不同的配置文件分割,将文件放到 /home/www/conf.d目录下,然后使用include进行合并
(4)为/server1和/server2各自创建一个访问日志文件

文件准备:
在这里插入图片描述

index_sr1_location1.html

<h1>this is sr1 location1 html</h1>

index_sr1_location2.html

<h1>this is sr1 location2 html</h1>

index_sr2_location1.html

<h1>this is sr2 location1 html</h1>

index_sr2_location2.html

<h1>this is sr2 location2 html</h1>

/usr/local/nginx/conf/nginx.conf 的配置参数详解

user  www;     # www用户
worker_processes  2;   # 配置两个工作进程

error_log  logs/error.log; # 配置 error_log的路径  logs的路径默认是在 /usr/local/nginx 安装目录下
pid logs/nginx.pid;    # 配置nginx 进程pid的路径,默认是在 logs/nginx.pid文件中
#daemon on;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
 accept_mutex on;    #解决惊群问题,防止worker_process 争抢
 multi_accept on;    # 允许一个工作进程,可以接收多个连接
 worker_connections  1024; # 运行一个工作进程最大的连接数为1024
 use epoll;
}


http {
    include       mime.types;  # 解决静态资源类型问题
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    log_format server1 '----->server1 access log format';   # 指定日志输出格式,把格式命名为server1 
    log_format server2 '----->server2 access log format';   # 指定日志输出格式,把格式命名为server2

    include /home/www/conf.d/*.conf;   # 引入conf.d下面的所有配置文件,server1.conf,server2.conf 将会被引入到这个位置
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


server1.conf

server{
     listen 8081;   # 8081端口才能访问
     server_name localhost;  ip地址
     access_log /home/www/myweb/server1/logs/access.log server1;  # 指定access_log的路径为 server1/logs/access.log 输出格式为 server1
     location /server1/location1{     #  ip:port/server1/location1 访问到该地址 
              root  /home/www/myweb;   # 配置访问的文件夹,该文件夹不一定是要上一级的文件夹,上几级的文件夹也可以
              index  index_sr1_location1.html; # 找到index_sr1_location1.html,找到第一个 index_sr1_location1.html 文件返回该文件
    }
    location /server1/location2{     # ip:port/server1/location2 访问到该地址 
            root  /home/www/myweb;   # 配置访问的文件夹,该文件夹不一定是要上一级的文件夹,上几级的文件夹也可以
            index index_sr1_location2.html;  # 找到index_sr1_location2.html,找到第一个 index_sr1_location2.html 文件返回该文件
         }
   error_page 404 /404.html;   # 如果遇到404 代码 返回
   location = /404.html {
        root /home/www/myweb;
        index 404.html;
      }
}

server2.conf

server{
     listen 8082;
     server_name localhost;
     access_log /home/www/myweb/server2/logs/access.log server2;
     location /server2/location1{
              root  /home/www/myweb;
              index  index_sr2_location1.html;
    }
    location /server2/location2{
            root  /home/www/myweb;
            index index_sr2_location2.html;
         }
   error_page 404 /404.html;
   location = /404.html {
        root /home/www/myweb;
        index 404.html;
      }
}

结果展示

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值