博客
关于我
nginx安装与配置
阅读量:791 次
发布时间:2023-02-15

本文共 3711 字,大约阅读时间需要 12 分钟。

Nginx 软件部署与配置指南

环境部署

部署环境为 CentOS 6.9,系统信息如下:

[root@moban tools]# cat /etc/redhat-releaseCentOS release 6.9 (Final)[root@moban scripts]# uname -r2.6.32-696.el6.x86_64[root@moban tools]# uname -mx86_64

官网下载

下载 Nginx 最新版本(例如 1.14.0):

wget http://nginx.org/download/nginx-1.14.0.tar.gz

官网地址:http://nginx.org/

安装依赖

安装必要的编译依赖:

yum install -y pcre-devel openssl-devel gcc

用户管理

创建非 привileged 用户(例如 www):

useradd -s /sbin/nologin -M www

编译安装

编译并安装 Nginx:

cd /server/tools/tarxf nginx-1.14.0.tar.gzcd nginx-1.14.0./configure --prefix=/application/nginx-1.14 --user=www --group=www --with-http_stub_status_module --with-http_ssl_modulemake && make install

创建软链接

创建 Nginx 的可执行文件软链接:

ln -s /application/nginx-1.14 /application/nginx

启动 Nginx

启动服务:

/application/nginx/sbin/nginx

主配置文件

优化主配置文件(nginx.conf):

sed -ri '/#|^$/d' /application/nginx/conf/nginx.conf

虚拟主机配置

单域名配置

编辑主配置文件:

vim /application/nginx/conf/nginx.conf

添加以下内容:

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.abc.com;        location / {            root html/www;            index index.html index.htm;        }    }}

测试配置:

/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx

创建首页文件:

mkdir -p /application/nginx/html/wwwcp /application/nginx/html/index.html /application/nginx/html/www/index.html

多域名配置

编辑主配置文件:

vim /application/nginx/conf/nginx.conf

添加以下内容:

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.abc.com;        location / {            root html/www;            index index.html index.htm;        }    }    server {        listen 80;        server_name bbs.abc.com;        location / {            root html/bbs;            index index.html index.htm;        }    }    server {        listen 80;        server_name blog.abc.com;        location / {            root html/blog;            index index.html index.htm;        }    }}

测试配置:

/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx

重载服务

重载 Nginx 服务:

/application/nginx/sbin/nginx -s reload

状态模块

检查状态模块是否启用:

/application/nginx/sbin/nginx -V

查看状态模块配置:

cat /application/nginx/conf/extra/status.conf

错误日志

配置错误日志:

error_log logs/error.log error;

重载服务后测试:

/application/nginx/sbin/nginx -t/application/nginx/sbin/nginx -s reload

浏览器访问

访问测试:

curl www.abc.comcurl bbs.abc.comcurl blog.abc.com

端口访问

配置端口访问:

server {    listen 8080;    server_name www.abc.com;    location / {        root html/www;        index index.html index.htm;    }}

测试访问:

curl www.abc.com:8080

IP 配置

基于 IP 的虚拟主机配置:

server {    listen 10.0.0.7:80;    server_name www.abc.com;    location / {        root html/www;        index index.html index.htm;    }}

测试监听 IP:

netstat -lnp | grep nginx

目录结构

默认目录结构:

/application/nginx/html/├── www/│   └── index.html├── bbs/│   └── index.html├── blog/│   └── index.html└── logs/    ├── error.log    └── access.log

location 语法

location 语法解析:

  • =:精确匹配
  • ^~:常规字符串匹配
  • ~:区分大小写
  • ~*:不区分大小写
  • !:否定匹配

示例配置:

location / {    return 401;}location / {    return 402;}location /documents/ {    return 403;}location ^~/images/ {    return 404;}location ~*\.gif|\.jpg|\.jpeg$ {    return 500;}

目录访问控制

限制目录访问:

location / {    root html/www;    index index.html index.htm;    allow 172.16.1.0/24;    deny all;}location /AV/ {    root html/www;    index index.html index.htm;    allow 172.16.1.0/24;    deny all;}

测试访问:

mkdir -p /application/nginx/html/www/AVecho "web01 www" > /application/nginx/html/www/AV/oldboy.html

通过以上配置,Nginx 可以实现多域名的高效负载均衡与访问控制。

转载地址:http://orcfk.baihongyu.com/

你可能感兴趣的文章
Netty工作笔记0012---Channel应用案例3
查看>>
Netty工作笔记0013---Channel应用案例4Copy图片
查看>>
Netty工作笔记0014---Buffer类型化和只读
查看>>
Netty工作笔记0015---MappedByteBuffer使用
查看>>
Netty工作笔记0016---Buffer的分散和聚合
查看>>
Netty工作笔记0018---Selector介绍和原理
查看>>
Netty工作笔记0019---Selector API介绍
查看>>
Netty工作笔记0020---Selectionkey在NIO体系
查看>>
Netty工作笔记0021---NIO编写,快速入门---编写服务器
查看>>
Netty工作笔记0022---NIO快速入门--编写客户端
查看>>
Vue踩坑笔记 - 关于vue静态资源引入的问题
查看>>
Netty工作笔记0024---SelectionKey API
查看>>
Netty工作笔记0025---SocketChannel API
查看>>
Netty工作笔记0026---NIO 网络编程应用--群聊系统1---编写服务器1
查看>>
Netty工作笔记0027---NIO 网络编程应用--群聊系统2--服务器编写2
查看>>
Netty工作笔记0028---NIO 网络编程应用--群聊系统3--客户端编写1
查看>>
Netty工作笔记0030---NIO与零拷贝原理剖析
查看>>
Netty工作笔记0032---零拷贝AIO内容梳理
查看>>
Netty工作笔记0033---Netty概述
查看>>
Netty工作笔记0034---Netty架构设计--线程模型
查看>>