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

}

}

}

}

相关文章
|
1月前
|
应用服务中间件 nginx Docker
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
这篇文章介绍了如何通过域名在本地访问虚拟机上的nginx服务,包括创建nginx容器、修改配置文件、修改本地host文件以及进行访问测试的详细步骤。文章提供了具体的Docker命令来创建并配置nginx容器,展示了配置文件的修改示例,说明了如何在本地系统的hosts文件中添加虚拟机IP和自定义域名,以及如何通过浏览器进行测试访问。
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
|
12天前
|
应用服务中间件 nginx
一文搞定Nginx配置RTMP!
一文搞定Nginx配置RTMP!
49 3
|
13天前
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
30 2
|
28天前
|
Ubuntu 应用服务中间件 Linux
在Linux中,如何配置Web服务器(如Apache或Nginx)?
在Linux中,如何配置Web服务器(如Apache或Nginx)?
|
29天前
|
缓存 负载均衡 应用服务中间件
【揭秘】nginx代理配置全攻略:从零到精通,一文带你玩转高效网络代理的秘密武器!
【8月更文挑战第22天】nginx是一款高性能的HTTP与反向代理服务器,支持代理服务、负载均衡及缓存等功能,有助于提升网站响应速度和安全性。首先需确保已安装nginx,可通过包管理器进行安装。安装后启动并确认nginx运行状态。接着编辑配置文件(通常位于`/etc/nginx/nginx.conf`),设置代理转发规则,例如指定目标服务器地址和请求头信息。配置完成后测试有效性并重新加载nginx以应用更改。可以通过部署简易HTTP服务器验证代理功能是否正常工作。此外,还可以通过扩展配置文件实现更复杂的代理需求,如基于路径的代理和SSL加密等。
166 2
|
1月前
|
Web App开发 应用服务中间件 网络安全
如何在 Apache 和 Nginx 上配置 OCSP Stapling
如何在 Apache 和 Nginx 上配置 OCSP Stapling
49 8
|
1月前
|
jenkins 应用服务中间件 持续交付
如何配置 Nginx 作为 Jenkins 的反向代理并启用 SSL 加密
如何配置 Nginx 作为 Jenkins 的反向代理并启用 SSL 加密
54 8
|
29天前
|
缓存 负载均衡 应用服务中间件
如何配置 NGINX 反向代理
【8月更文挑战第21天】
130 0
如何配置 NGINX 反向代理
|
1月前
|
存储 Ubuntu 应用服务中间件
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
17 4
|
1月前
|
负载均衡 应用服务中间件 网络安全
NGINX配置详解
NGINX配置详解