Nginx安装与配置【转】
原文:linux之nginx
作者;海燕。
一、nginx
Ngix是web服务器,跟apache一样,它可以做动态请求转发、web端负载均衡、反向代理等等;
tomcat是应用服务器,当然如果非用逼良为娼,也可做web服务器用,它主要是做Servlet容器用的,一般用在应用层,运行后台逻辑代码,访问数据库服务器等; 一般常见的网站,采用apache+tomcat+数据库或是Ngix+tomcat+数据库这三层物理架构;如果是大型网站应用,上面还会有一层硬件负载均衡F5。 从性能角度来讲Ngix的性能似乎要比apache略好,但从使用方便上来看,apache配置起来更方便,功能上都很强大,因为apache名气似乎更大一些,用的人就更多一些。
1、下载

[root@s6haiyanvm ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
ll -h nginx-1.12.2.tar.gz #查看多大
tar xf nginx-1.12.2.tar.gz #解压
cd nginx-1.12.2.tar.gz
ls -l
12一般偶数版的是稳定版本 ,基数是测试版

2、编译安装nginx前的准备
useradd -s /sbin/nplogin -M www #添加一个用户,-s和-M是吧用户变成虚拟用户号
id www
su - www
3、安装nginx的依赖包

#pcre-devel perl语言的兼容正则表达式
#openssl-devel https
yum install pcre-devel -y
yum install openssl-devel -y
rpm -qa pcre-devel

4、编译安装nginx

pwd #应该是在这个路径下/application/nginx-1.12.2
./configure --help
(1)./configure --user=www --group=www --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module
--user :用户
--group : 用户组
--prefix :安装到哪里
两个模块: --with-http_stub_status_module --with-http_ssl_module
tree -L echo $? #表示以上一次的执行情况 ,返回0表示正确,返回1表示错误
(2)make #根据一些配置进行编译
(3)make install md5sum nginx/1.12.2.tar.gz #给这个压缩包创建个指纹,相当于身份证

5、查看

/application/nginx-1.12.2/sbin/nginx -t #检查是否语法错误
/application/nginx-1.12.2/sbin/nginx #执行命令
ss -lntup|grep 80 #查看安装好了没有
10.0.0.200 #在浏览器里面看页面,看搭建好了没有
curl -v 10.0.0.200 #在linux里面查看

6、nginx的常用配置

[root@oldboyedu-s6 nginx-1.12.2]# pwd
/application/nginx-1.12.2
[root@oldboyedu-s6 nginx-1.12.2]# ll
total 36
drwx------ 2 www root 4096 Mar 15 10:31 client_body_temp
drwxr-xr-x 2 root root 4096 Mar 15 10:00 conf nginx配置文件的目录
drwx------ 2 www root 4096 Mar 15 10:31 fastcgi_temp
drwxr-xr-x 2 root root 4096 Mar 15 10:00 html nginx站点目录 网站的根目录
drwxr-xr-x 2 root root 4096 Mar 15 10:31 logs nginx日志
drwx------ 2 www root 4096 Mar 15 10:31 proxy_temp
drwxr-xr-x 2 root root 4096 Mar 15 10:00 sbin nginx命令
drwx------ 2 www root 4096 Mar 15 10:31 scgi_temp
drwx------ 2 www root 4096 Mar 15 10:31 uwsgi_temp
├── client_body_temp
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf #nginx的主配置文件
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp
├── html
│ ├── 50x.html
│ └── index.html #网站默认的首页文件
├── logs
│ ├── access.log #访问日志
│ ├── error.log
│ └── nginx.pid
├── proxy_temp
├── sbin
│ └── nginx
├── scgi_temp
└── uwsgi_temp


#修改配置文件
egrep '#' #查看包含#的 egrep '^$' #空行
egrep '^$|#' conf/nginx.conf #排除空行或者#
egrep '^$|#' conf/nginx.conf.default > conf/nginx.conf

修改了配置文件记得重启一下
/application/nginx-1/sbin/nginx -s reload #优雅的重启
/application/nginx-1/sbin/nginx -s stop #优雅的停止(迫不得已才用的) /application/nginx-1/sbin/nginx
二、搭建一个小网站
1、修改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.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

2.创建环境
mkdir -p /application/nginx-1.12.2/html/{www,bbs,blog} #创建多级目录
for name in www bbs blog;do echo $name.etiantian.org> /application/nginx-1.12.2/html/$name/index.html ;done
for name in www bbs blog;do cat /application/nginx-1.12.2/html/$name/index.html ;done
3.检查语法并重启
[root@oldboyedu-s6 nginx-1.12.2]# /application/nginx-1.12.2/sbin/nginx -t
nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful
[root@oldboyedu-s6 nginx-1.12.2]# /application/nginx-1.12.2/sbin/nginx -s reload
4.windows测试 浏览器(注意缓存)

(1)方式一:cat /etc/hosts #修改host,域名解析
10.0.0.200 www.etiantian.org bbs.etiantian.org blog.etiantian.org
www.eitiantian.org #浏览器输入测试
(2)方式二:
curl -H Host:www.etiantian.org 10.0.0.200 #修改host里面的host

5、排错流程:

1.linux命令行是否能显示
curl -vH Host:www.etiantian.org 10.0.0.200 2.windows 本地shell
ping

6、搭建多个

[root@oldboyedu-s6 nginx-1.12.2]# cat 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.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
} } [root@oldboyedu-s6 nginx-1.12.2]# curl -H Host:bbs.etiantian.org 10.0.0.200
bbs.etiantian.org
[root@oldboyedu-s6 nginx-1.12.2]# curl -H Host:blog.etiantian.org 10.0.0.200
blog.etiantian.org

三、参数location的理解

“~”用于区分大小写(大小写敏感)的匹配; ~ /images {}
“~*” 用于不区分大小写的匹配。还可以用逻辑操作符!对上面的匹配取反,即!~ 和 !~*。
“^~”作用是在常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的那个字符串匹配的location配置中有此前缀,那么不做正则表达式的检查。


[root@oldboyedu-s6 nginx-1.12.2]# cat 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.etiantian.org etiantian.org;
root html/www;
location / {
return 401;
} location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
}
} ^~
~*
/documents/
/ 第1名:“location ~* \.(gif|jpg|jpeg)$ {” 正则匹配
第2名:“location /documents/ {” 匹配常规字符串,如果有正则则优先匹配正则。
第3名:“location / {” 所有location都不能匹配后的默认匹配。 [root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Thu, 15 Mar 2018 04:13:41 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive [root@oldboyedu-s6 nginx-1.12.2]#
[root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200/documents/index.html
HTTP/1.1 403 Forbidden
Server: nginx/1.12.2
Date: Thu, 15 Mar 2018 04:14:42 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive [root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200/documents/w.jpg
HTTP/1.1 500 Internal Server Error
Server: nginx/1.12.2
Date: Thu, 15 Mar 2018 04:15:56 GMT
Content-Type: text/html
Content-Length: 193
Connection: close [root@oldboyedu-s6 nginx-1.12.2]#
[root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200/images/www.jpg
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Thu, 15 Mar 2018 04:16:52 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Nginx安装与配置【转】的更多相关文章
- 阿里云服务器Linux CentOS安装配置(八)nginx安装、配置、域名绑定
阿里云服务器Linux CentOS安装配置(八)nginx安装.配置.域名绑定 1.安装nginx yum -y install nginx 2.启动nginx service nginx star ...
- ubuntu server nginx 安装与配置
ubuntu server nginx 安装与配置 一:关于nginx http://wiki.ubuntu.org.cn/Nginx http://nginx.org/cn http://wiki. ...
- Nginx安装及配置详解【转】
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- [转帖]Nginx安装及配置详解 From https://www.cnblogs.com/zhouxinfei/p/7862285.html
Nginx安装及配置详解 nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP ...
- Linux中Nginx安装与配置详解
转载自:http://www.linuxidc.com/Linux/2016-08/134110.htm Linux中Nginx安装与配置详解(CentOS-6.5:nginx-1.5.0). 1 N ...
- centos7系统下nginx安装并配置开机自启动操作
准备工作 我的centos7系统是最小化安装的, 缺很多库, 首先安装必须的运行库 ? 1 2 3 4 5 6 7 8 9 10 11 yum install wget gcc gcc-c++ pcr ...
- linux nginx安装以及配置
一.Nginx简介 Nginx (“engine x”) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- Nginx安装以及配置
安装编译工具及库文件 1 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 安装 PCRE 下载 PC ...
- Nginx安装与配置-Centos7
Nginx是一款高性能免费开源网页服务器,也可用于反向代理和负载均衡服务器.该软件由伊戈尔·赛索耶夫于2004年发布,2019年3月11日,Nginx被F5 Networks以6.7亿美元收购.201 ...
- LVS+Nginx(LVS + Keepalived + Nginx安装及配置)
(也可以每个nginx都挂在上所有的应用服务器) nginx大家都在用,估计也很熟悉了,在做负载均衡时很好用,安装简单.配置简单.相关材料也特别多. lvs是国内的章文嵩博士的大作,比nginx被广 ...
随机推荐
- 关于Intel处理器架构中AVX2里Gather特性的说明
在Intel Haswell架构里引入了Gather特性.它使得CPU可以使用向量索引存储器编址从存储器取非连续的数据元素.这些gather指令引入了一种新的存储器寻址形式,该形式由一个基地址寄存器( ...
- ES6深入浅出_汇总贴
H:\BaiDu\ES6深入浅出-wjw ES 6 新特性一览:https://frankfang.github.io/es-6-tutorials/ 我用了两个月的时间才理解 let https:/ ...
- Qt编写安防视频监控系统4-删除视频
一.前言 一般会有两种处理方式来删除视频,一种是鼠标右键菜单,删除当前视频或者删除所有视频,一种是直接按住当前视频,移到视频通道界面以外就表示删除当前视频,这也是个比较人性化的设置,每个人的喜好不一样 ...
- 深入理解Flink ---- Metrics的内部结构
从Metrics的使用说起 Flink的Metrics种类有四种Counters, Gauges, Histograms和Meters. 如何使用Metrics呢? 以Counter为例, publi ...
- [C++/JavaScript]数据结构:栈和数列>案例引入(数制的转换)
1 案例1:数制的转换 1.1 背景与原理 1.2 编程复现 (JavaScript版 复现) function convert(value, d){ stack = []; // 栈 result ...
- 实现两个DataTable的联合查询
如方法一描述:将子表的数组追加到主表数组的下面.从而实现类似于视图(单表)的效果. 那么Left Join(Inner Join)和Right Join(Outer Join) 将如何实现呢? 明天仔 ...
- webstorm关闭vim模式
- 【ARM-Linux开发】cmem模块/DVSDK2.0
1. CMEM--高速缓存一致性问题的解决多核设计中,共享的二级高速缓存之间数据可能不一致,不同CPU内核的私有高速缓存也可能存在数据不一致,称为高速缓存的一致性问题.解决一致性问题的方法从整体上分可 ...
- outlook寻找/删除指定日期范围内的邮件
总是收到很多系统预警邮件,时间久了攒了好多垃圾邮件.实际上只需保存近期预警邮件,之前的完全可以删除. 上网找了一圈也没找到方法,然后自己想到了一种,步骤如下: 使用outlook规则,将指定日期范围内 ...
- JavaSE基础(五)--Java运算符
Java 运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运算符 ...