您的位置:首页 > 运维架构 > Nginx

nginx笔记二:nginx 安装和基本应用

2018-11-06 23:40 274 查看

lvs FWM及持久连接.note模块类型:
核心模块:core module
标准模块:
Standard HTTP modules 标准模块
Optional HTTP modules 可选模块
Mail modules 邮件模块
3rd party modules: 第三方模块
1,每个模块必须引用指令
2,每个模块都引入 变量
nginx用来做什么?
静态资源的web服务器
http协议反向代理

epel源地址:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/OS/OSERLEASE/$basearch/
gpgchek=0
enabled=1

nginx的安装:

如果编译安装还需要安装的开发组件:
#yum groupinstall "Development Tools" "Sever Platforment" -y

yum -y install pcre-devel openssl-devel zlib-devel

./configure\

--prefix=/usr/local
--sbin-path=/usr/sbin/nginx
--conf-path=/etc/nginx/nginx.conf
--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
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx
--group=nginx
--with-http_ssl_module
--with-http_realip_module
--with-http_addition_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_stub_status_module
--with-http_auth_request_module
--with-threads
--with-stream
--with-stream_ssl_module
--with-http_slice_module
--with-mail
--with-mail_ssl_module
--with-file-aio
--with-http_v2_module

make && make install

cd /usr/local/nginx/

#ls

ls sbin/

#ls html/

/usr/local/nginx/sbin/nginx -t 测试语法是否错误

/usr/local/nginx/sbin/nginx -h 获得当前帮助信息

/usr/local/nginx/sbin/nginx 启动nginx

/usr/local/nginx/sbin/nginx -s 立即停止nginx

/usr/local/nginx/sbin/nginx -s relaod 重启 5b28

测试:浏览器输入:IP
#ps aux 查看进程

cd /etc/nginx/ 进入配置文件

cp nginx.conf{,.bak} 备份配置文件

ls

配置:
配置文件的组成部分:
主配置文件:nginx.conf
include conf.d/*.conf
facscgi, scgi, uwscgi的相关配置
mime.types 配置文件

配置指令(必须以分号结尾);否则语法错误
directive value1 [value2...];    固定格式

支持使用变量:
内置变量:有模块引入,可直接引用;
自定义变量:set variable_name value;
引用变量:$variable_name

常见的内置变量:
$args, 此变量与请求行中的的参数相等,
$document_root, 此变量等同于当前请求的root 命令指定的值。
$uri, 此变量等同于当前request中的URI.
$document_uri, 此变量与$uri含义一样
$host, 此变量与请求头部中”Host“ 行指定的值一致;
$limit_rate, 此变量用来设置限制连接的速率。
$request_method, 此变量等同于request的method, 通常是“GET” 或者“POST”,
$remote_port, 此变量表示客户端的端口。
$request_user, 此变量等同于用户名,有ngx_http_auth_basic_module认证;
$remote_addr, 此变量表示客户端IP地址
$request_filename, 此变量表示当前请求的文件的路径名,有root或者alias与URI request组合而成
$request_uri, 此变量表示含有参数完整的初始路径;URI
$query_string, 此变量与$args含义一致。
$request_name,此变量表示请求到达的服务器名,
$server_port, 此变量表示请求到达的服务器端口号;

配置文件结构:
main block: 对http及mail模块均有效;全局配置;
event {
...
} : 事件驱动的相关配置;
http{
...
} :http协议的相关配置
mail {
...
}mail相关的配置;

http相关的配置:
http相关的配置:虚拟主机
http {
...
...
server {
...
server_name
root         根文件
alias
location /url/ {          定义路径
...
}
...
}
server {
...
...
}
}

/////////////////////////////////////////////////////////////////////////////////////////////
实例:
server {
lister 80;
server_name www.tb.com;
access_log logs/host.access.log main;
location / {
root /var/www/html; #根目录
index index.html index.htm;
}
location ~.(gif|jpg|jpeg|png|bmp|htm|html|css|js)$ { #静态资源在下面三个目录
root /usr/local/nginx/www/img;
if (!-f $request_filename)
{
root /var/www/html/img;
}
if (!-f $request_filename)
}
root /apps/images;
}
}
location ~.(jsp)$ { #动态资源在下面连个目录
root /webdata/webapp/www/ROOT;
IF (!-f $request_name)
{
root /usr/local/nginx/www/jsp;
}
proxy_pass http://127.0.0.1:8888;
}
}
这段代码主要对www.tb.com这个域名的资源访问配置,www.tb.com这个域名的根目录为“/var/www/html”, 而静态资源位置分别位于“/usr/local/nginx/www/img”, "/var/www/html/img", "/apps/images' 三个目录下,请求静态资源方式依次在这三个目录下找,如果在第一个下面找不到,就在第二个下面找,如果都找不到就显示404;
动态资源分别位于“/webdata/webapp/www/ROOT”和"/usr/local/nginx/www/jsp" 两个目录下,如果客户端请求的资源是以jsp结尾的文件,那么将依次在这两个动态程序目录下查找资源,而对于没有在这两个目录下定义的资源,将全部从更目录 “/var/www/html”进行查找
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

main block:
配置指令的类别:
正常运行必备的配置;
优化性能的配置;
用于调试、定位问题的配置;

正常运行必备的配置:
1、user USERNAME [GROUPNAME];
指定用于运行worker进程的用户和组;
user nginx nginx;
2、pid /PATH/TO/PID_FILE;
指定nginx进程的pid文件路径;
pid /var/run/nginx.pid;
3、worker_rlimit_nofile number;
单个worker进程所能够打开的最大文件数;
性能优化相关的配置:
1、worker_processes number | auto;
worker的进程数; 通常应该为CPU的核心数减1;

2、worker_cpu_affinity cpumask ...;
worker_cpu_affinity auto [cpumask];

例:# worker_processes 2 ;

worker_cpu_affinity 0010 0100;

cpu绑定

ps axo command, pid, psr

CPUMASK:
0000  0001
0000  0010
0000  0100
0000  1000
。。。。

2、worker_priority nice;指明进程优先级别
[-20, 19]        nice 越小就优先级就越高
100-139
例如:worker_priority -5;

调试、定位问题;
1、daemon on | off;
是否以守护进程方式启动nginx进程;

2、master_process on | off;
是否以master/worker模型启动nignx进程;

3、error_log file | stderr | syslog:server=address[,parameter=value] |                                                                            memory :size [devug | info | notice | warn | error | crit | alert | emerg];
错误日志文件的记录方式;及其日志级别;
方式:
file /PATH/TO/SOME_LOG_FILE;
stderr : 发送到错误输出;
syslog:server=address[,parameter=value]:发送给syslog服务器;
memory:size
日志级别:
debug依赖于configure时的--with-debug选项;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  运维 基础 服务