文章目录
Linux 环境安装并配置Nginx
前言
还是前段时间的那个项目,生产环境因为是配置了域名的nginx,结果从客户现场回来之后发现测试环境没有域名就,也没安装,安装的时候遇到一定的问题,下面讲一讲Linux环境如何安装nginx以及如何配置域名
安装前置条件
检查是否已安装nginx (若已安装,跳过下面这个步骤,直接停止即可
## 检查默认安装路径/usr/local/nginx/
cd /usr/local/nginx/sbin
## 执行
./nginx -V ##若想要升级版本的话可以考虑先卸载再安装
## 如果不是默认安装路径的话,可以通过查看进程的方式查看自己安装的nginx路径
ps -aux | grep nginx
# 或者
ps -ef | grep nginx
# 或者
whereis nginx
停止nginx(若没有启动,跳过下面一步)
# 方式一:进到检查默认安装路径停止
/usr/local/nginx/sbin/nginx -s stop
# 方式二 通过查找nginx的进程杀进程的方式停止
# 查找nginx的进程
ps -ef| grep nginx
# 或
ps -aux| grep nginx
## 强制停止nginx
kill -9 pid
彻底删除nginx
查找根目录下所有名字包含nginx的文件
find / -name nginx
全局查找往往会查出很多相关文件,但是前缀基本都是相同,后面不同的部分可以用*代替,以便快速删除
rm -rf /usr/local/sbin/nginx
rm -rf /usr/local/nginx
rm -rf /usr/src/nginx-1.11.1
rm -rf /var/spool/mail/nginx
如果设置了Nginx开机自启的话,可能还需要下面步骤
chkconfig nginx off
rm -rf /etc/init.d/nginx
安装包下载
http://nginx.org/en/download.html
可点击此处跳转
推荐下载稳定版
正式安装nginx
安装nginx依赖的软件库
yum install -y gcc openssl openssl-devel zlib zlib-devel pcre-devel
将刚刚从官网上下载下来的nginx包上传到服务器上并解压(推荐放到home的目录下)
tar -zxvf 文件名
进入到解压的nginx文件夹下依次执行以下命令
./configure
make
make install
如果安装的过程中出现下面这个错误
src/os/unix/ngx_user.c:26:7: 错误:‘struct crypt_data’没有名为‘current_sal
说明nginx的版本太低了,或者是linux版本太高了,可以尝试降低Linux版本或者下载高版本nginx
启动/停止/重启nginx
进入 /usr/local/nginx/sbin
# 启动
./nginx
# 重启
./nginx -s reload
# 停止 方式一
./nginx -s stop
# 停止 方式二
pkill -9 nginx
# 停止 方式三 可以参考上面查nginx的进程号的方法
kill -9 进程号
验证是否已安装成功
如果安装好了之后没有任何改动的话nginx默认是监听80端口
浏览器访问的话如果nginx没有另设监听端口的话也是访问80端口
因此输入服务器的外网访问IP即可验证
nginx配置详解
默认config配置
#user nobody;
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 {
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 on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
nginx文件结构
... #全局块
events { #events块
...
}
http #http块
{
... #http全局块
server #server块
{
... #server全局块
location [PATTERN] #location块
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局块
}
1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
5、location块:配置请求的路由,以及各种页面的处理情况。
示例讲解
下面写一个模拟案例
########### 每个指令必须有分号结束。#################
#user administrator administrators; #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:6208;
server 192.168.134.174:9909 backup; #热备
}
upstream rstsvr {
server 127.0.0.1:9905 weight=10; #权重10 访问网站30次此服务被访问10次
server 192.168.134.174:38080 weight=20; #权重20 访问网站30次此服务被访问20次
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口 默为80
server_name 127.0.0.1; #监听地址 或者 域名
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 192.168.134.176; #允许的ip
}
location ^~/rstsvr { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://rstsvr; #请求转向rstsvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 192.168.134.176; #允许的ip
}
}
}
上面是nginx的基本配置,需要注意的有以下几点
1、基本配置
1. r e m o t e a d d r 与 remote_addr 与 remoteaddr与http_x_forwarded_for 用以记录客户端的ip地址;
2. r e m o t e u s e r : 用 来 记 录 客 户 端 用 户 名 称 ; 3. remote_user :用来记录客户端用户名称; 3. remoteuser:用来记录客户端用户名称;3.time_local : 用来记录访问时间与时区;
4. r e q u e s t : 用 来 记 录 请 求 的 u r l 与 h t t p 协 议 ; 5. request : 用来记录请求的url与http协议; 5. request:用来记录请求的url与http协议;5.status : 用来记录请求状态;成功是200;
6. b o d y b y t e s s e n t : 记 录 发 送 给 客 户 端 文 件 主 体 内 容 大 小 ; 7. body_bytes_s ent :记录发送给客户端文件主体内容大小; 7. bodybytessent:记录发送给客户端文件主体内容大小;7.http_referer :用来记录从那个页面链接访问过来的;
8.$http_user_agent :记录客户端浏览器的相关信息;
2、惊群现象
一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。
3、每个指令必须有分号结束。
小齐也是边摸索边加配置,后续再有具体案例再补充
分享一句我特别喜欢的话:每一天都是一个新的日子,走运当然是好的,不过我情愿做到分毫不差。这样,运气来的时候,你就有准备了。 ————海明威《老人与海》