nginx在Linux的安装和简单使用
文章目录
- 安装
- 下载安装包和依赖
- 安装
- nginx的配置文件
- 反向代理配置
- nginx 配置实例-负载均衡
- Nginx 提供了几种分配方式(策略):
- nginx 配置实例-动静分离
- 反向代理和静态服务器的配置规律
- 配置代理缓存
- TCP转发
安装
下载安装包和依赖
浏览器中输入http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
即可下载
下载nginx的安装包
http://nginx.org/en/download.html
安装
tar –xvf pcre-8.37.tar.gz
解压压缩文件- 进入pcre-8.37目录执行
./configure
命令 - 安装 openssl 、zlib 、 gcc 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
- 安装 nginx
使用命令解压nginx安装包并进入nginx安装包目录 - 执行
./configure
命令后,执行make && make install
命令 - 进入
/usr/local/nginx/sbin
目录启动nginx,使用./nginx
命令 - 通过
http://ip/
访问,可访问到说明可以启动成功 - 关闭nginx使用
./nginx -s stop
命令 - 重新加载nginx使用
./nginx -s reload
命令
nginx的配置文件
cd /usr/local/nginx/conf/
路径下的nginx.conf文件
配置文件中的内容
包含三部分内容
- 全局块:配置服务器整体运行的配置指令
比如 worker_processes 1;处理并发数的配置 - events 块:影响 Nginx 服务器与用户的网络连接
比如 worker_connections 1024; 支持的最大连接数为 1024 - http 块
还包含两部分:
http 全局块
server 块
反向代理配置
- 监听端口,如果监听到就转向另外的端口
监听80端口,转向8080端口
访问http://192.168.64.123:80
将请求转发到http://192.168.64.123:8080/
,配置中的proxy_pass为代理地址,被代理的地址要以/结尾,否则地址不是到要代理的ip和端口上
server {listen 80; # 监听端口server_name localhost; #监听ip#charset koi8-r;#access_log logs/host.access.log main;location / {root html;proxy_pass http://192.168.64.123:8080/; #代理地址 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;#}
}
- 根据访问的路径跳转到不同端口的服务中
nginx 监听端口为 9001,
访问http://192.168.64.123:9001/edu/
直接跳转到http://192.168.64.123:8081/edu/
访问http://192.168.64.123:9001/vod/
直接跳转到http://192.168.64.123:8080/vod/
配置
server {listen 9001;#listen somename:8080;server_name 192.168.64.123;location ~ /edu/ {proxy_pass http://192.168.64.123:8081;}location ~ /vod/ {proxy_pass http://192.168.64.123:8080;}
}
配置中的location ~ /vod/正则表达式说明
- = :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配
成功,就停止继续向下搜索并立即处理该请求。 - ~:用于表示 uri 包含正则表达式,并且区分大小写。
- ~*:用于表示 uri 包含正则表达式,并且不区分大小写。
- ^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字
符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location块中的正则 uri 和请求字符串做匹配。
nginx 配置实例-负载均衡
修改配置文件
events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;upstream myserver{server 192.168.64.123:8080;server 192.168.64.123:8081;}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;proxy_pass http://myserver;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}server {listen 9001;#listen somename:8080;server_name 192.168.64.123;location ~ /edu/ {proxy_pass http://192.168.64.123:8081;}location ~ /vod/ {proxy_pass http://192.168.64.123:8080;}}
}
在原有基础上添加了,注意每行server的最后都要加上; ,否则会报错
upstream myserver{server 192.168.64.123:8080;server 192.168.64.123:8081;
}
proxy_pass http://myserver; http后面的路径为upstream 后面的参数
Nginx 提供了几种分配方式(策略):
- 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除 - weight
weight 代表权,重默认为 1,权重越高被分配的客户端越多
指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况 ,如
upstream server_pool{server 192.168.5.21 weight=10;server 192.168.5.22 weight=10;
}
- ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。 例如:
upstream server_pool{ip_hash;server 192.168.5.21:80;server 192.168.5.22:80;
}
- 、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。比如
upstream server_pool{server 192.168.5.21:80;server 192.168.5.22:80;fair;
}
nginx 配置实例-动静分离
配置文件案例
events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;upstream myserver{server 192.168.64.123:8080;server 192.168.64.123:8081;}server {listen 80;server_name localhost;location / {root html;proxy_pass http://myserver;index index.html index.htm;}location /img/ {root /soft/;autoindex on;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}# another virtual host using mix of IP-, name-, and port-based configuration#server {listen 9001;#listen somename:8080;server_name 192.168.64.123;location ~ /edu/ {proxy_pass http://192.168.64.123:8081;}location ~ /vod/ {proxy_pass http://192.168.64.123:8080;}}
}
新增的配置为
location /img/ {root /soft/;autoindex on; # 可访问文件目录
}
访问http://192.168.64.123/img/1.jpg可以访问到服务器的/soft/img路径下的图片
也可以用alias别名代理,配置为
location /img/ {alias /soft/img/;autoindex on; # 可访问文件目录
}
反向代理和静态服务器的配置规律
可以把配置中的location后面的匹配路径作为分隔符,匹配到了后将请求路径中分隔符前面的部分替换为root
或者proxy_pass
后面的配置参数生成一个新的路径即为代理路径或者静态页面的路径
如果用alias
,则匹配到了后是将匹配路径作为分隔符,将请求路径的分隔符及前面的路径全部替换了alias后面的参数,与root和proxy_pass的区别是拼成的新路径带不带匹配路径
配置代理缓存
proxy_cache_path /soft/nginx_cache/ levels=1:2 keys_zone=cache_image:200m inactive=1d max_size=6g; # 缓存配置server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;# location / {# root html;# proxy_pass http://myserver;# index index.html index.htm;# }location / {root html;# autoindex on;proxy_pass http://192.168.1.123:8080; # 代理地址,必须为http的,不能是本地路径/dev/tmp这种proxy_cache_valid 200 304 1h; # 有效时间proxy_cache cache_image; # 值为上面的配置的# proxy_cache_methods GET POST;add_header Nginx-Cache "$upstream_cache_status"; # 响应头部信息,如果使用的缓存则值为HIT,否则为MISS}#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;#}
}
TCP转发
nginx从1.9版本开始支持TCP、UDP转发,但是需要编译时带上--with stream
安装好nginx后安装stream流程
- 在nginx-1.18.0目录下执行
./configure --with-stream
- 执行
make && make install
完成上面两个即可配置TCP转发,我因为用的安装都是nginx默认的,所以执行以上两个命令即可
配置转发rabbitmq请求为例
# stream模块和http模块平级,即是最外层
stream{server{listen 10002;proxy_pass 192.168.1.123:5672;}
}
nginx在Linux的安装和简单使用相关推荐
- nginx在linux下安装,Nginx在linux下安装及简单命令
安装环境:Centos7 创建目录及切换至目录 # mkdir /usr/local/nginx # cd /usr/local/nginx/ wget http://nginx.org/downlo ...
- linux下安装nginx启动,Linux下安装启动nginx的过程
1.首先将nginx的安装包传到虚拟机里的/home目录下 2.为了方便nginx运行而不影响linux安全需创建组合用户 groupadd -r nginx useradd -r -g nginx ...
- linux tahoma字体,Linux字体安装与简单美化(实践篇)(转)
Linux字体安装与简单美化(实践篇)(转)[@more@](注:本文以Debian GNU/Linux为蓝本.各版本中个别路径用法等不同请自行查看修改.) 1.首先拷贝字体到系统字体目录下比如/us ...
- LNMP架构环境搭建之PHP、Nginx源码编译安装及其简单配置应用
LNMP架构中的Mysql见上一篇博文"LNMP架构环境搭建之mysql源码编译安装" 一.PHP简介 PHP(外文名:PHP: Hypertext Preprocessor,中文 ...
- Python基础学习1(Python的Windows和Linux的安装及简单学习)
一Python的安装 1.Windows下安装Python (1)windows 命令行的几个常见的命令 dir:查看当前目录下的所有文件,以及目录 cd + NAME:进入到NAME目录下(tab键 ...
- linux没有桌面安装svn,Linux下安装SVN简单教程
安装 使用yum安装非常简单: yum install subversion 配置 2.1. 创建仓库 我们这里在/home下建立一个名为svn的仓库(repository),以后所有代码都放在这个下 ...
- nginx在linux上安装,Nginx在linux上安装步骤
Nginx在centos7上的安装过程 #安装前的准备工作 centos7系统环境已经安装好,linux处于能联网状态,且linux和自己window能相互ping同. 首先安装make,编译环境gc ...
- linux nginx 代理iis,nginx 系列 linux下安装以及配置IIS分发
一. 安装 操作系统:centos 7 ,nginx版本1.12.2,windows server 2008 iis 1.1 确认nginx所依赖的工具 Zlib: nginx提供gzip模块,需要 ...
- Redis之在Linux上安装和简单的使用
一.安装gcc 1.Redis在linux上的安装首先必须先安装gcc,这个是用来编译redis的源文件的.首先需要先切换的到root用户: [cheny@localhost ~]$ su Passw ...
最新文章
- 计算机与plc链接通信协议,实现上位计算机与PLC的上位链接系统的通信设计
- MySQL 跨库分页/ 分表分页/ 跨库分页,为什么这么难?
- .NET Core微服务之基于Consul实现服务治理
- css网页布局中文字排版的属性和用法
- LIST-PROCESSING命令的使用
- Android 小技巧
- python能和c语音交互吗_Python和C语言交互--ctypes,struct
- React.Component与React.PureComponent的区别
- 前端中对象的几种创建方式
- HTML 父窗口打开子窗口,并从子窗口返回值
- memcache 客户端性能对比试验
- 14. 随需应变:网站的可扩展架构
- bootstrap-select 插件使用详解
- 基于Kinetis系列微控制器K60芯片的I2C接口函数程序说明1
- 万能解压器安卓版_全能压缩软件下载
- matlab定积分例子,利用Matlab进行不定积分运算示例巧妙至极.doc
- chrome 清理缓存
- PR中视频材料声音大小不一样?1招快速统一音量
- 北京链家网租房信息的数据分析项目实战
- MAC苹果电脑关闭系统完整性保护SIP(System Integrity Protection)