在CentOS7(mini)上安装:

[root@~ localhost]#lftp 172.16.0.1
lftp 172.16.0.1:/pub/Sources/7.x86_64/nginx> pwd
ftp://172.16.0.1/pub/Sources/7.x86_64/nginx
#偶数版本号为稳定版
lftp 172.16.0.1:/pub/Sources/7.x86_64/nginx> get nginx-1.10.2-1.el7.ngx.x86_64.rpm nginx-module-geoip-1.10.2-1.el7.ngx.x86_64.rpm
670448 bytes transferred
Total 2 files transferred
[root@~ localhost]#ls
anaconda-ks.cfg nginx-1.10.2-1.el7.ngx.x86_64.rpm nginx-module-geoip-1.10.2-1.el7.ngx.x86_64.rpm
[root@~ localhost]#yum install ./nginx-1.10.2-1.el7.ngx.x86_64.rpm
#80端口没启动,即可启动Nginx
[root@~ localhost]#ss -ntl
[root@~ localhost]#systemctl start nginx.service

[root@etc localhost]#cd /etc/nginx/
[root@nginx localhost]#vim nginx.conf
[root@nginx localhost]#cd conf.d/
[root@conf.d localhost]#ls
default.conf
[root@conf.d localhost]#vim test.conf
server {
listen 8080;
server_name www.magedu.com;
location / {
index index.html index.htm;
root /data/www;
} } [root@conf.d localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重载
[root@conf.d localhost]#nginx -s reload
[root@conf.d localhost]#mkdir -pv /data/www
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/www’
[root@conf.d localhost]#vim /data/www/index.html <h1>test page</h1>
[root@conf.d localhost]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:8080 *:*

动态装载动态模块:

  load_module files;

#动态装载Geoip模块
[root@~ localhost]#yum -y install ./nginx-module-geoip-1.10.2-1.el7.ngx.x86_64.rpm
#查看安装完成生成的文件
[root@~ localhost]#rpm -ql nginx-module-geoip
/usr/lib64/nginx/modules/ngx_http_geoip_module-debug.so
/usr/lib64/nginx/modules/ngx_http_geoip_module.so #此文件是需要加载的模块
/usr/share/doc/nginx-module-geoip
/usr/share/doc/nginx-module-geoip/COPYRIGHT
#编辑主配置文件,将需要加载的文件放到全局配置中(main配置)
[root@~ localhost]#vim /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; load_module /usr/lib64/nginx/modules/ngx_http_geoip_module.so; #装载模块 #配置文件语法检查
[root@~ localhost]#nginx -t
#重载
[root@~ localhost]#nginx -s reload

性能优化的相关配置:

#进程数量和进程绑定CPU
[root@~ localhost]#vim /etc/nginx/nginx.conf
user nginx;
worker_processes 4; #按CPU的颗数-lscpu
#将worker进程绑定在指定的CPU上
worker_cpu_affinity 0001 0010 0100 1000;
[root@~ localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful #将进程没有绑定在CPU和绑定进行压测
#没有绑定CPU
[root@nginx localhost]#vim nginx.conf
worker_processes 4;
#worker_cpu_affinity 0001 0010 0100 1000;
[root@nginx localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx localhost]#nginx -s reload
#检测
#-n:每0.5秒检测一次
#psr:运行进程的CPU
[root@nginx localhost]#watch -n 0.5 'ps axo pid,comm,psr|grep "nginx"' Every 0.5s: ps axo pid,comm,psr|grep "nginx" Mon Jun 5 13:59:29 2017 20628 nginx 0
20766 nginx 3
20767 nginx 1
20768 nginx 0
20769 nginx 0 #另一台服务器测试:
[root@~ localhost]#yum -y install httpd-tools
#-n:发起的请求数
#-c:并发数
[root@~ localhost]#ab -n 100000 -c 20 http://172.16.250.89/index.html Benchmarking 172.16.250.89 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests Document Path: /index.html
Document Length: 612 bytes Concurrency Level: 20
Time taken for tests: 189.948 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 84500000 bytes
HTML transferred: 61200000 bytes
Requests per second: 526.46 [#/sec] (mean)
Time per request: 37.990 [ms] (mean)
Time per request: 1.899 [ms] (mean, across all concurrent requests)
Transfer rate: 434.43 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 16 6.9 16 423
Processing: 2 21 9.4 20 552
Waiting: 0 15 8.9 15 407
Total: 6 37 13.7 36 652 Percentage of the requests served within a certain time (ms)
50% 36
66% 37
75% 39
80% 40
90% 43
95% 46
98% 53
99% 77
100% 652 (longest request) #进程绑定CPU:
[root@nginx localhost]#vim nginx.conf
user nginx;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000; [root@nginx localhost]#watch -n 0.5 'ps axo pid,comm,psr|grep "nginx"'
[root@nginx localhost]#vim nginx.conf
[root@nginx localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx localhost]#nginx -s reload
[root@nginx localhost]#watch -n 0.5 'ps axo pid,comm,psr|grep "nginx"' Every 0.5s: ps axo pid,comm,psr|grep "nginx" Mon Jun 5 14:03:10 2017 20628 nginx 0
23847 nginx 0
23848 nginx 1
23849 nginx 2
23850 nginx 3 #压测:
[root@~ localhost]#ab -n 100000 -c 20 http://172.16.250.89/index.html Benchmarking 172.16.250.89 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests Document Path: /index.html
Document Length: 612 bytes Concurrency Level: 20
Time taken for tests: 186.953 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 84500000 bytes
HTML transferred: 61200000 bytes
Requests per second: 534.89 [#/sec] (mean)
Time per request: 37.391 [ms] (mean)
Time per request: 1.870 [ms] (mean, across all concurrent requests)
Transfer rate: 441.39 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 17 4.9 16 194
Processing: 3 20 6.6 19 308
Waiting: 0 14 5.7 14 204
Total: 12 37 9.9 35 457 Percentage of the requests served within a certain time (ms)
50% 35
66% 37
75% 38
80% 39
90% 43
95% 48
98% 55
99% 66
100% 457 (longest request)

配置文件中映射目录测试:

[root@images localhost]#vim /etc/nginx/conf.d/test.conf
server {
listen 8080;
server_name www.magedu.com;
location / {
index index.html index.htm;
root /data/www;
}
location /images/ { #此处的路径实际上是/data/pictures/images;
root /data/pictures/;
} } [root@images localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@images localhost]#nginx -s reload
[root@images localhost]#mkdir /data/pictures
[root@images localhost]#cd /data/pictures
[root@pictures localhost]#ls
[root@pictures localhost]#mkdir images
[root@pictures localhost]#ls
images
[root@pictures localhost]#cd images/
[root@images localhost]#ls
tulips.jpg
[root@images localhost]#pwd
/data/pictures/images
[root@images localhost]#ls
tulips.jpg

#将root换成alias时
[root@pictures localhost]#vim /etc/nginx/conf.d/test.conf
[root@images localhost]#cd /data/pictures/
[root@pictures localhost]#ls
images lighthouse.jpg
[root@pictures localhost]#vim /etc/nginx/conf.d/test.conf
server {
listen 8080;
server_name www.magedu.com;
location / {
index index.html index.htm;
root /data/www;
}
location /images/ {
alias /data/pictures/;        #别名,实际上相当于/images/与/data/pictures同级
} }
[root@pictures localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@pictures localhost]#nginx -s reload

没有location的测试:

[root@conf.d localhost]#pwd
/etc/nginx/conf.d
[root@conf.d localhost]#vi test2.conf
server {
listen 8088; #开启8088端口
root /data/www2;
server_name www2.magedu.com;
} [root@conf.d localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@conf.d localhost]#nginx -s reload
[root@conf.d localhost]#mkdir /data/www2
[root@conf.d localhost]#vim /data/www2/index.html
<h1>www2</h1>

错误页面的重定向:

[root@conf.d localhost]#cat test2.conf
server {
listen 8088;
server_name www2.magedu.com;
#错误页面定位到如下路径;即在/data/www2/下得有这个图片文件
error_page 404 http://172.16.250.89:8088/error.jpg;
location / {
root /data/www2; }
}
[root@www localhost]#cd /data/www2/
[root@www2 localhost]#ls
error.jpg images index.html

输入http://172.16.250.89:8088/index2.html:

用Telnet测试keepalive:

[root@conf.d localhost]#cat test2.conf
server {
listen 8088;
server_name www2.magedu.com;
error_page 404 =200 http://172.16.250.89:8088/error.jpg;
keepalive_timeout 60; #60s后断开长连接
keepalive_requests ; #在单个长连接上最多响应10个资源
location / {
root /data/www2; }
} [root@~ localhost]#telnet 172.16.250.89 8088 #指明ip和端口
Trying 172.16.250.89...
Connected to 172.16.250.89.
Escape character is '^]'.
GET /index.html HTTP/1.1   #Telnet测试语法
Host: 172.16.250.89
Connection closed by foreign host. #超过60s自动断开长连接

basic认证:

[root@nginx localhost]#vim /etc/nginx/conf.d/test.conf 

server {
listen 8080;
server_name www.magedu.com;
location / {
index index.html index.htm;
root /data/www;
}
location /images/ {
alias /data/pictures/;
}
location /bbs/ {
root /data/; #需要认证的文件
auth_basic "BBS internal"; #认证说明
auth_basic_user_file /etc/nginx/.ngxpasswd; #认证文件
}
}
#安装httpd-tools使用htpasswd生成认证文件和用户
[root@nginx localhost]#yum -y install httpd-tools
[root@nginx localhost]#htpasswd -c -m /etc/nginx/.ngxpasswd tom
#-c:创建认证文件
#-m:md5的算法加密
[root@nginx localhost]#htpasswd -m /etc/nginx/.ngxpasswd jerry
[root@nginx localhost]#nginx -t
[root@nginx localhost]#nginx -s reload
[root@nginx localhost]#mkdir /data/bbs
[root@nginx localhost]#vim /data/bbs/index.html
<h1>FBI area</h1>

输入tom 和密码:

输出stub_status:

[root@nginx localhost]#vim /etc/nginx/conf.d/test.conf
location /ngxstatus {
stub_status;
}
[root@nginx localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx localhost]#nginx -s reload

启用压缩功能:

[root@nginx localhost]#vim nginx.conf
gzip on; #启用压缩
[root@nginx localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginx localhost]#nginx -s reload
[root@www localhost]#curl -I --compressed http://172.16.250.89:8080/access_log.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 14 Jun 2017 13:13:19 GMT
Content-Type: text/html
Last-Modified: Wed, 14 Jun 2017 12:43:59 GMT
Connection: keep-alive
ETag: W/"59412f8f-14ea216"
Content-Encoding: gzip #已压缩 [root@nginx localhost]#vim nginx.conf
gzip on;
gzip_types text/css text/plain application/javascript;
gzip_comp_level 5; #压缩比
gzip_min_length 64; #启用压缩的最小长度
[root@nginx localhost]#nginx -t
[root@nginx localhost]#nginx -s reload

ngx_http_rewrite_module:

[root@conf.d localhost]#vim test.conf
error_page 404 =200 /images/error.jpg;
location / {
index index.html index.htm;
root /data/www;
if ($http_user_agent ~* curl) {
rewrite ^(.*)$ /curl/$1; #curl测试重定向
}
}
location /curl/ {
alias /data/textagent; #curl重定向后的文件路径,需要手动创建
}
#对error_page做重定向,到百度首页,临时的重定向(redirect:302)
location = /images/error.jpg {
rewrite ^(.*)$ http://www.baidu.com/ redirect;
}
#新疆curl重定向的页面
[root@conf.d localhost]#cd /data/
[root@data localhost]#cat textagent/index.html
<h1>hello,this is rewrite page</h1> [root@conf.d localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@conf.d localhost]#nginx -s reload

测试效果:

输入:

ngx_http_referer_module:防盗链机制;

[root@conf.d localhost]#cat test.conf
#在浏览器非手动键入网址访问或者referer值为空时,跳转
valid_referers none blocked server_name *.magedu.com;
if ($invalid_referer) {
return 302 http://www.magedu.com/;
}
[root@~ localhost]#curl -I -e http://www.baidu.com http://172.16.250.89:8080/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.10.2
Date: Wed, 14 Jun 2017 16:03:04 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: http://www.magedu.com/ #跳转到定义的网址
#-I:只显示请求头信息
#-e:模拟referer,从那个网址直接跳转到本网址

ngx_http_proxy_module:

[root@conf.d localhost]#cat default.conf
server {
listen 80;
server_name localhost; #charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main; location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://192.168.2.129:80/bbs; #此ip是被代理的主机ip
}

输入:

#proxy_set_header:为了让被代理的的主机能保存真正访问的信息,如访问客户端的ip信息
[root@conf.d localhost]#vim default.conf
location ~* \.(jpg|jpeg|gif|png)$ {
proxy_pass http://192.168.2.129;
proxy_set_header X-Real-IP $remote_addr;
}
[root@~ localhost]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@~ localhost]#nginx -s reload
#在被代理主机上设置:
[root@html localhost]#vim /etc/httpd/conf/httpd.conf
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@html localhost]#httpd -t
[root@html localhost]#systemctl restart httpd

[root@html localhost]#tail /var/log/httpd/access_log
172.16.250.55 - - [06/Jun/2017:13:09:12 +0800] "GET /1.jpg HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
172.16.250.55 - - [06/Jun/2017:13:09:14 +0800] "GET /1.jpg HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
#172.16.250.55 是访问代理主机的客户端信息

代理缓存的设置:

[root@~ localhost]#cd /etc/nginx/
[root@nginx localhost]#vim nginx.conf
http {
#定义proxy的缓存路径必须在http下
#level是定义目录的格式,一级目录两个十六进制的值,二级是一个,三级是一个;keys_zone的名称是可以自由定义的,大小最大的1G
proxy_cache_path /data/cache/ngnix levels=2:1:1 keys_zone=mycache:10m max_size=1g;
}
[root@nginx localhost]#mkdir /data/cache/ngnix -pv
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/cache’
mkdir: created directory ‘/data/cache/ngnix’
[root@nginx localhost]#vim /etc/nginx/conf.d/default.conf
location ~* \.(jpg|jpeg|gif|png)$ {
proxy_pass http://192.168.2.129;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache mycache; #引用定义的缓存
proxy_cache_key $request_uri; #缓存key的内容
proxy_cache_valid 200 301 302 10m; #对于特定响应码的缓存时长 m分钟 h 小时
proxy_cache_valid any 1m; } [root@nginx localhost]#nginx -t
[root@nginx localhost]#nginx -s reload

测试:

[root@conf.d localhost]#ll /data/cache/ngnix/ -h
total 16K
drwx------ 3 nginx nginx 4.0K Jun 6 00:22 60
drwx------ 3 nginx nginx 4.0K Jun 6 00:17 a4
drwx------ 3 nginx nginx 4.0K Jun 6 00:22 b3
drwx------ 3 nginx nginx 4.0K Jun 6 00:22 dd
[root@conf.d localhost]#ll /data/cache/ngnix/60/4/c/635450b5c68b27bfaa2e0065cb86c460 -h
[root@nginx localhost]#vim /etc/nginx/conf.d/default.conf

location ~* \.(jpg|jpeg|gif|png)$ {
proxy_pass http://192.168.2.129;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache mycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 301 302 10m;
proxy_cache_valid any 1m;
proxy_hide_header Server; #隐藏的是后端服务器的首部信息,而不是Nginx代理服务器的首部信息
add_header X-Proxy $server_addr; #添加首部的展示信息 }

输入:

nginx的安装及基本配置的更多相关文章

  1. Nginx的安装及相关配置

    Nginx的安装及相关配置 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境. 一. gcc 安装 安装 n ...

  2. Nginx的安装及简单配置

    Nginx安装 1.下载相关组件 yum install -y gcc gcc-c++                                   #安装C/C++编译器 yum -y ins ...

  3. nginx 编译安装以及简单配置

    前言 Nginx的大名如雷贯耳,资料太多了,网上一搜一大把,所以这里就不阐述nginx的工作原理了,只是简单的编译安装nginx,然后呢,简单配置一下下. 下载Nginx.安装 下载地址:http:/ ...

  4. Ubuntu16 Nginx的安装与基本配置

    关于Nginx 它是一个轻量级.高性能.稳定性高.并发性好的HTTP和反向代理服务器,当我们搭建自己的应用时,通常用它作为反向代理服务器,图片服务器和负载均衡. 1.Ubuntu 16安装 Nginx ...

  5. java架构之路-(nginx使用详解)nginx的安装和基本配置

    Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和Unix的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的Unix工具软件.应用程序和网络协议.它支持32位 ...

  6. Nginx 2.安装与部署配置

    转 https://www.cnblogs.com/wcwnina/p/8728430.html > 下载 官方网站:https://nginx.org/en/download.html Win ...

  7. Linux下nginx的安装以及环境配置

    参考链接 https://blog.csdn.net/qq_42815754/article/details/82980326 环境: centos7 .nginx-1.9.14 1.下载 并解压  ...

  8. Nginx服务安装配置

    1.Nginx介绍 Nginx是一个高性能的HTTP和反向代理服务器,由俄罗斯人开发的,第一个版本发布于2004年10月4日.Nginx由于出色的性能,在世界范围内受到了越来越多人的关注,其特点是占有 ...

  9. Django Nginx+uwsgi 安装配置

    使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttp ...

随机推荐

  1. Swift学习笔记十一:方法

         方法是与某些特定类型相关联的功能/函数.在Swift中,结构体和枚举能够定义方法:其实这是Swift与C/Objective-C的主要差别之中的一个. 在Objective-C中,类是唯一能 ...

  2. (转)基于libRTMP的流媒体直播之 AAC、H264 推送

    参考: 1,基于libRTMP的流媒体直播之 AAC.H264 推送 http://billhoo.blog.51cto.com/2337751/1557646

  3. 关于align-items和align-content的区别和使用场景

    最近在研究flex布局,容器中有两个属性,是用来定义crossAxis测轴排列方式的.一开始接触align-items还可以理解感觉不难,后来看到align-content就感觉有点混淆了,特开一篇博 ...

  4. Python 面试题(上)

    Python语言特性 1 Python的函数参数传递 看两个例子: a = 1 deffun(a): a = 2 fun(a) printa # 1 a = [] deffun(a): a.appen ...

  5. STM32 ~ MDK环境下调试程序 HardFault_Handler 相关

    STM32出现HardFault_Handler故障的原因主要有两个方面: 1.内存溢出或者访问越界.这个需要自己写程序的时候规范代码,遇到了需要慢慢排查. 2.堆栈溢出.增加堆栈的大小. 出现问题时 ...

  6. Redis缓存全自动安装shell脚本

    我只是把命令放到shell文件中了,方便安装,代码如下: #!/bin/bash # shell的执行选项: # -n 只读取shell脚本,但不实际执行 # -x 进入跟踪方式,显示所执行的每一条命 ...

  7. Python decorator @property

    @property广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性 下面的链接很好的阐述了@property的概念和应用 http: ...

  8. 【leetcode刷题笔记】Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  9. INT(M)表示什么意思?

    根据官方文档描述,int(M)中的M表示数据显示的宽度,与实际存储的长度无关. 1.也就是int(3)和int(11)能够存储的数据是一样的,都是从-2147483648到2147483647(或者0 ...

  10. 从mysqldump整库备份文件中恢复单表

    最近,系统更新出现了问题,比较紧急,需要对三张表进行回档.由于我们都是采用mysqldump进行每天全备整库,数据量比较大,一个备份文件大概有70G,需要从这个70G文件中恢复三张表,真是蛋疼至极啊, ...