nginx 基本配置 | 学习笔记(二)

简介: 快速学习 nginx 基本配置。

开发者学堂课程【Linux Web 服务器 Nginx 搭建与配置nginx 基本配置学习笔记,与课程紧密联系,让用户快速学习知识.

课程地址:https://developer.aliyun.com/learning/course/579/detail/7989


nginx 基本配置(二)


五、nginx的安装

1.官方:

http://nginx.org/packages/centos/7/x86 64/RPMS

2.Fedora-EPEL:

https://mirrors.aliyun.com/epel/7/x86 64/

3.编译安装:

  • yum install pcre-devel openssl-devel zlib-deve
  • useradd -r nginx
  • ./configure--prefix=/usr/local/nginxconf--

error-log-path=/var/log/nginx/error.log--http-log-

path=/var/log/nginx/access.log--pid--path=/var/run/nginx.pid --lock- path=/var/run/nginx.lock--user=nginx --group=nginx--with-http_ssl_module

--with-http_v2 module --with-http dav module --with-http_stub_status_module --with-threads --with-file-aio

  • make && make install

以下为稳定版

Pre-Built Packages for Stable version

To set up the yum repository for RHEL/CentOS, create the file named /etc/tum.repos.d/nginx.repo with the following contents:

[nginx]

Name=nginx repo

Baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/

gpgcheck=0

enabled=1

Replace“os”with “rhel” or “centas”, depending on the distribution used, and“OSRELEASE”with "6” or “7”.for 6.x or 7.x versions, respectively

For Debian/Ubuntu, in order to authenticate the nginx repository signature and to eliminate warnings about missing PGP key during installation of the nginx package, it is necessary to add the key used to sign the nginx packages and repository to the apt program keyring. Please download this key from our web site, and add it to the apt program keyring with the following command:

sudo apt-key add nginx_signing.key

For Debian replace codename with Debian distribution cedename, and append the following to the end of the /etc/apt/sourcea.list file:

deb http://nginx.org/packages/debian/ codename nginx

deb-src http://nginx.org/packages/debian/ codename nginx

For Ubuntu replace codename with Ubuntu distribution codename, and append the following to the end of the /etc/apt/sources, list file;

deb http://nginx.org/packages/ubuntu/ codename nginx

deb-src http://nginx.org/packages/ubuntu/ codename nginx

For Debian/Ubun

路径中可进行适当替换

【Ngink】

name=nginx repo

bseur1=http://nginx.org/packages/centos/7/x86_64/

gpgcheck=0

enabled=1

此时为已读取效果

image.png

下面按直接用压路源进行安装,其中/etc/nginx/nginx.conf为主要配置文件,之后也是对其进行修改。接下来配置nginx,

[root@centos7~]#

[root@centos7~]#

[root@centos7~]#which nginx

usr/sbin/nginx

[root@centos7~]#nginx=-help

nginx:invalid ootiont:”-“

[root@centos7~]#nginx_j

Nginx:inva;id option:”

[root@centos7~]#nginx-h

nainx version:nginx/1.12.2

Usage:ngink ThVVtTqI L-Ssigna1] [-c filename] [-p prefix] [-gdirectives]

Options:

-?.-h :this help

-V:show version and exit

-v :show versionandconfioureoptions then exit

-T:test confiouration and exit

-t  :test Cestcontiquration . dump it and exit

s signal  :send signal to a master process : stop quit reopen reload

-p prefix :set prefix path (default:/usr/share/nginx/)

-cfilename:set configuration file (default:/ect?nginx/nginx.conf)

-g directives  :set global directives out of configuration file

我们一般用[ront@centos7~]#nginx -s stop这个来启动期服务,但用过后要使用相应的进行停止


六、nginx 目录结构和命令

Is /usr/local/nginx/

html是测试页,sbin是主程序

Is /usr/local/nginx/sbin/

nginx 只有一个程序文件

ls /usr/local/nginx/html/

50x.html index.html 测试网页

Nginx:默认为启动nginx

-h查看帮助选项

-V 查看版本和配置选项

-t 测试nginx语法错误

-c filename 指定配置文件

(default:/etc/nginx/nginx.conf)

-s signal 发送信号给master进程,

signal : stop, quit, reopen,reload

示例:nginx -s stop 停止nginx

nginx -s reload 加载配置文件

-g directives在命令行中指明全局指令


七.nginx 配置

1. 配置文件的组成部分

主配置文件:nginx.conf

子配置文件 include conf.d/t.conf

fastcgi,uwsgi,scgi等协议相关的配置文件mime.types:支持的mime类型

主配置文件的配置指令:

directive value [value2...];

注意:

(1)指令必须以分号结尾

(2) 支持使用配置变量

内建变量:由Nginx模块引入,可直接引用自定义变量:由用户使用set命令定义

set variable_name value;

内置模块很多,可以参考官网,之后具体讲述

2.Main-全局配置段常见的配置指令分类

user nginx;

Worker processes auto:

error -log/var/log/nginx/ercor.log;

pid /run/nginx.pid

#Load dynamic modules.See/usr/share/nginx/README.dynanic

Events  {

worker -connections 1024

{

这是一个全局式的设置,它影响的是所有服务,http服务、邮件服务、转化代理服务……

正常运行必备的配置

优化性能相关的配置

用于调试及定位问题相关的配置

事件驱动相关的配置

帮助文档

http://nginx.org/en/docs/

正常运行必备的配置:

帮助文档:http://nginx.org/en/docs/ngx_core_module.html

1. user

Syntax: user user [group];

Default: user nobody nobody;

Context: main

指定

worker进程的运行身份,如组不指定,默认和用户名同名

2、pid /PATH/TO/PID FILE

指定存储nginx主进程PID的文件路径

3.include file I mask

指明包含进来的

其它配置文件片断

4.load_module file

模块加载配置文件:/usr/share/nginx/modules/*.conf

指明要装载的动态模块路径:/usr/lib64/nginx/modules


八、nginx 配置文件

主配置文件结构:四部

main block(主块):主配置段,即全局配置段,对http,mail都有效event {

} 事件驱动相关的配置

http{

}  http/https 协议相关配置段

mail {

}     mail 协议相关配置段

stream {

}    stream 服务器相关配置段

官方目前提供的只有main block和HTTP这两大块,其余需要自己手动填写。


九、http 协议相关的配置结构

http {

…各server的公共配置

server { 每个server用于定义一个虚拟主机

}

server {

servername 虚拟生机名

root   主目录

alias   路径别名

location [OPERATOR] URL {  指定URLB的特性

if CONDITION I

}

}

}

}

相关文章
|
2月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
298 1
|
6月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
720 87
|
6月前
|
负载均衡 应用服务中间件 nginx
Nginx配置与命令
Nginx 是一款高性能的 HTTP 和反向代理服务器,其配置文件灵活且功能强大。本文介绍了 Nginx 配置的基础结构和常用指令,包括全局块、Events 块、HTTP 块及 Server 块的配置方法,以及静态资源服务、反向代理、负载均衡、HTTPS 和 URL 重写等功能实现。此外,还提供了常用的 Nginx 命令操作,如启动、停止、重载配置和日志管理等,帮助用户高效管理和优化服务器性能。
|
2月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
257 18
|
2月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
322 17
|
3月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
4月前
|
应用服务中间件 网络安全 nginx
配置Nginx以支持Websocket连接的方法。
通过上述配置,Nginx将能够理解WebSocket协议的特殊要求,代理Websocket流量到合适的后端服务器。注意,Websocket并不是HTTP,尽管它最初是通过HTTP请求启动的连接升级,因此保证Nginx了解并能够妥善处理这种升级流程是关键。
1092 10
|
3月前
|
Ubuntu 应用服务中间件 Linux
在Ubuntu上配置Nginx实现开机自启功能
至此,Nginx应该已经被正确地设置为开机自启。在Ubuntu中利用 `systemd`对服务进行管理是一种高效的方式,为系统管理员提供了强大的服务管理能力,包括但不限于启动、停止、重启服务,以及配置服务的开机自启动。通过这些简洁的命令,即使是对Linux不太熟悉的用户也能轻松地进行配置。
179 0
|
5月前
|
安全 应用服务中间件 网络安全
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
362 0
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
|
5月前
|
JSON 前端开发 应用服务中间件
配置Nginx根据IP地址进行流量限制以及返回JSON格式数据的方案
最后,记得在任何生产环境部署之前,进行透彻测试以确保一切运转如预期。遵循这些战术,守卫你的网络城堡不再是难题。
252 3