linux nginx安装与nginx配置详解

来自:网络
时间:2019-02-12
阅读:

1.什么是Nginx

Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。 其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易腾讯,另外知名的微网志Plurk也使用nginx。

优点:

(1)Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。 Nginx 的1.2.6稳定版已经于2012年12月11日发布,[1]1.3.10开发版已经于2012年12月25日发布,如果新建站点,建议使用最新稳定版作为生产版本,已有站点升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。

(2)Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:

在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

(3)Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 RAIls 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。

作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last. fm 描述了成功并且美妙的使用经验。

(4)Nginx 是一个安装非常的简单,配置文件非常简洁(还能够支持perl语法),Bugs非常少的服务器:Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。

2,下载Nginx

[root@localhost nginx]# wget http://nginx.org/download/nginx-1.7.8.tar.gz
--2018-06-29 16:54:32--  http://nginx.org/download/nginx-1.7.8.tar.gz
正在解析主机 nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2001:1af8:4060:a004:21::e3, ...
正在连接 nginx.org (nginx.org)|95.211.80.227|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:823122 (804K) [application/octet-stream]
正在保存至: “nginx-1.7.8.tar.gz”
100%[=================================================================================================>] 823,122      231KB/s 用时 3.5s   
2018-06-29 16:54:36 (231 KB/s) - 已保存 “nginx-1.7.8.tar.gz” [823122/823122])

下载依赖的插件

[root@localhost opt]# yum install pcre
已加载插件:fastestmirror, langpacks
[root@localhost opt]# yum install pcre-devel
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 pcre-devel.x86_64.0.8.32-17.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
[root@localhost opt]# yum install zlib
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
软件包 zlib-1.2.7-17.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@localhost opt]# yum install zlib-devel
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 zlib-devel.x86_64.0.1.2.7-17.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决

解压Nginx

[root@localhost nginx]# tar -xvzf nginx-1.7.8.tar.gz -C /usr/local/src/

配置 

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre

可以通过“./configure  --help”选项设置各个模块的使用情况,例如对不需要的http_ssl模块,可通过“--without-http_ssl_module”方式关闭此模块,在configure选项中“--with-http_stub_status_module”可以用来启用 Nginx 的 NginxStatus 功能,以监控 Nginx 的当前状态

配置编译报错 ./configure: error: C compiler cc is not found

checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

原因是缺少gcc-c++

[root@localhost nginx-1.7.8]# yum install gcc-c++
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc-c++.x86_64.0.4.8.5-28.el7_5.1 将被 安装

编译安装

[root@localhost nginx-1.7.8]# make && make install

检查是否安装成功

[root@localhost ]#cd  /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3,配置Nginx配置

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
#开启进程数 <=CPU数
worker_processes 1;
#错误日志保存位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#进程号保存文件
#pid logs/nginx.pid;
#等待事件
events {
#每个进程最大连接数(最大连接=连接数x进程数) 
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#日志文件输出格式 这个位置相于全局设置
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#请求日志保存位置
#access_log logs/access.log main;
#打开发送文件
sendfile on;
#tcp_nopush on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#打开gzip压缩
#gzip on;
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#设定负载均衡的服务器列表
upstream myproject { 
#weigth参数表示权值,权值越高被分配到的几率越大
#max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
#fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
#这里指定多个源服务器,ip:端口,80端口的话可写可不写 
server 192.168.1.78:8080 weight=5 max_fails=2 fail_timeout=600s;
#server 192.168.1.222:8080 weight=3 max_fails=2 fail_timeout=600s; 
}
#第一个虚拟主机
server {
#监听IP端口
listen 80;
#主机名
server_name localhost;
#设置字符集
#charset koi8-r;
#本虚拟server的访问日志 相当于局部变量
#access_log logs/host.access.log main; 
#对本server"/"启用负载均衡
location / { 
#root /root; #定义服务器的默认网站根目录位置
#index index.php index.html index.htm; #定义首页索引文件的名称
proxy_pass http://myproject; #请求转向myproject定义的服务器列表
#以下是一些反向代理的配置可删除.
# proxy_redirect off; 
# proxy_set_header Host $host; 
# proxy_set_header X-Real-IP $remote_addr; 
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
# client_max_body_size 10m; #允许客户端请求的最大单文件字节数 
# client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数, 
# proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) 
# proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) 
# proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) 
# proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 
# proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 
# proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) 
# proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
} 
location /upload { 
alias e:/upload; 
}
#设定查看Nginx状态的地址 
location /NginxStatus { 
stub_status on; 
access_log off; 
#allow 192.168.0.3;
#deny all;
#auth_basic "NginxStatus"; 
#auth_basic_user_file conf/htpasswd; 
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
} 
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#多监听 
# listen 8000;
#主机名
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
#WEB文件路径
# root html;
#默认首页
# index index.html index.htm;
# }
#}
# HTTPS server HTTPS SSL加密服务器
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#} 
}

启动Nginx

[root@localhost sbin]# ./nginx
首次启动需:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost sbin]# ./nginx -s reload


开通80防火墙端口

[root@xxxx]# firewall-cmd --add-port=80/tcp --permanent
success
[root@xxx]# firewall-cmd --reload
success

查看防火墙是否开启

[root@localhost init.d]# /sbin/iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0            udp dpt:53
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:53
...
Chain IN_public_allow (1 references)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3306 ctstate NEW
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 ctstate NEW

验证服务是否启动

停止操作
停止操作是通过向nginx进程发送信号(什么是信号请参阅linux文 章)来进行的
步骤1:查询nginx主进程号
ps -ef | grep nginx
在进程列表里 面找master进程,它的编号就是主进程号了。
步骤2:发送信号
从容停止Nginx:
kill -QUIT 主进程号
快速停止Nginx:
kill -TERM 主进程号
强制停止Nginx:
pkill -9 nginx

[root@localhost sbin]# ps -ef | grep nginxroot      37590      1  0 11:16 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    39096  37590  0 11:38 ?        00:00:00 nginx: worker process
root      39134   3161  0 11:39 pts/0    00:00:00 grep --color=auto nginx
[root@localhost sbin]# netstat -na | grep 80tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp6       0      0 :::8080                 :::*                    LISTEN     
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN     
tcp6       0      0 :::8009                 :::*                    LISTEN

最后浏览器访问http://localhost:80 出现欢迎页面就OK了

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.
返回顶部
顶部