nginx性能高,而nginx的高性能与其架构是分不开的。那么nginx究竟是怎么样的呢?正好公司要求在服务器上配置nginx,我就借着这个时间好好研究一下。公司的服务器是windows的,但是研究一次,总得研究个透吧。所以windows和centos我都研究测试了,今天总算全通了,整理记录一下,以备后用。
注:仅负载均衡,读写分离更麻烦,以后再研究。
一、windows版本
windows版本的比较简单,只需要下载好,配置文件写好,运行就可以了。
1、nginx下载链接:这是windows版本的下载链接
2、下载之后解压,win + R ,启动cmd命令提示符,在解压路径下运行。
2.1 手太生,记录一下详细操作过程。(我的解压目录是D:\ToolForWork\nginx-1.15.2)
C:\Users\Administrator>d:
D:\>cd ToolForWork
D:\ToolForWork>cd nginx-1.15.2
D:\ToolForWork\nginx-1.15.2>start nginx
3、nginx在windows下的常用命令
3.1 启动
D:\ToolForWork\nginx-1.15.2>start nginx
3.2 停止
D:\ToolForWork\nginx-1.15.2>nginx -s quit
或者 D:\ToolForWork\nginx-1.15.2>nginx -s stop
注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。
3.3 重启
D:\ToolForWork\nginx-1.15.2>nginx -s reload
3.4 查看Nginx版本
D:\ToolForWork\nginx-1.15.2>nginx -v
二、centos版本
centos实在是不熟,所以整个过程实在是曲折。但是!我岂是能轻易被打败的!所以,最后还是搞出来了。现在将我遇到的坑一一记录。
1、首先是虚拟机固定ip,转:VMware虚拟机中CentOS/redhat设置固定IP
我是采用的桥接模式设置固定ip
2、linux版下载链接:这是linux版本的下载链接
3、安装过程:
3.1、下载Nginx及相关组件
Linux系统是Centos 6.5 64位,我直接切换到root用户下安装
进入用户目录下载程序
下载相关组件
[root@localhost src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
省略安装内容...
[root@localhost src]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
省略安装内容...
[root@localhost src]# wget http://zlib.net/zlib-1.2.11.tar.gz
省略安装内容...
[root@localhost src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
省略安装内容...
安装c++编译环境,如已安装可略过
[root@localhost src]# yum install gcc-c++
省略安装内容...
期间会有确认提示输入y回车
Is this ok [y/N]:y
省略安装内容...
3.2、安装Nginx及相关组件
openssl安装
[root@localhost src]# tar zxvf openssl-fips-2.0.10.tar.gz
省略安装内容...
[root@localhost src]# cd openssl-fips-2.0.10
[root@localhost openssl-fips-2.0.10]# ./config && make && make install
省略安装内容...
pcre安装
[root@localhost src]# tar zxvf pcre-8.40.tar.gz
省略安装内容...
[root@localhost src]# cd pcre-8.40
[root@localhost pcre-8.40]# ./configure && make && make install
省略安装内容...
zlib安装
[root@localhost src]# tar zxvf zlib-1.2.11.tar.gz
省略安装内容...
[root@localhost src]# cd zlib-1.2.11
[root@localhost zlib-1.2.11]# ./configure && make && make install
省略安装内容...
nginx安装
[root@localhost src]# tar zxvf nginx-1.10.2.tar.gz
省略安装内容...
[root@localhost src]# cd nginx-1.10.2
[root@localhost nginx-1.10.2]# ./configure && make && make install
省略安装内容...
3、启动Nginx
先找一下nginx安装到什么位置上了
进入nginx目录并启动
报错了,error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory,按照下面方式解决
1.用whereis libpcre.so.1命令找到libpcre.so.1在哪里
2.用ln -s /usr/local/lib/libpcre.so.1 /lib64命令做个软连接就可以了
3.用sbin/nginx启动Nginx
4.用ps -aux | grep nginx查看状态
[root@localhost nginx]# whereis libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
[root@localhost nginx]# ps -aux | grep nginx
进入Linux系统的图形界面,打开浏览器输入localhost会看到下图,说明nginx启动成功
nginx的基本操作
启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重启
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令帮助
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
验证配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.con
4、简单配置Nginx
打开nginx配置文件位于nginx目录下的conf文件夹下
简单介绍一下vim的语法
默认vim打开后是不能录入的,需要按键才能操作,具体如下:
开启编辑:按“i”或者“Insert”键
退出编辑:“Esc”键
退出vim:“:q”
保存vim:“:w”
保存退出vim:“:wq”
不保存退出vim:“:q!”
三、nginx配置文件:nginx.conf
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#配置服务器集群
upstream tomcat_server{
server localhost:8081 weight=1;
server localhost:8080 weight=2;
}
server{
#监听端口号
listen 8090;
server_name localhost;
location / {
proxy_pass http://tomcat_server;
}
}
upstream tomcat_Robot_server{
server localhost:8082;
server 192.168.0.251:8083;
}
server {
listen 8091;
server_name www.test.com;
location / {
proxy_pass http://tomcat_Robot_server;
}
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置很简单,但是整个摸索花了一两天,总算跑通了。记录一下,留待后用。后期深入研究读写分离等等再行更新。
有错误的地方还请指出。