Linux安装Nginx

更新时间:2023-03-16 01:16:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

nginx rewrite依赖于PCRE库,所以需要在linux系统中编译安装PCRE库。具体步骤如下:

1.下载PCRE包

首先去官网下载pcre的安装包 如果通过FTP的方式,下载地址为:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

如版本为8. 37,linux对应的安装包名称为:pcre-8. 37.tar.gz。 1)tar -zxvf pcre-8. 37.tar.gz 2)cd pcre-8. 37

3)./configure --enable-utf8 4)make

5) make check

6) make install(ubuntu 如果不是root用户需要用sudo执行)

2.安装Ngingx

例如nginx-1.8.1.tar.gz 1)tar -zxvf nginx-1.8.1.tar.gz 2)cd nginx-1.8.1.

3)./configure --prefix=/home/qianlinlai/nginx-1.8.1 --conf-path=/home/qianlinlai/nginx-1.8.1/ nginx.conf --without-http_memcached_module --with-pcre=/home/qianlinlai/pcre-8.37 --with-http_stub_status_module

--without-http_gzip_module(根据实际需要增删相应模块 ssl gzip等需要单独下载后再配置) 4)make 5)make install

3.配置nginx.conf 主要参数:

#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] \ # '$status $body_bytes_sent \ # '\

#access_log logs/access.log main;

sendfile on; #tcp_nopush on;

#keepalive_timeout 0; keepalive_timeout 65;

#gzip on;

server {

listen 80;

server_name xx.xxx.cn;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

proxy_pass http://127.0.0.1:8888;//转发服务地址 对应tomcat端口 proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Cookie $http_cookie;//转发http头信息,解决会话问题 } } }

如上则配置好了一个转发服务:

访问http:// xx.xxx.cn则会转发到本地8888端口的tomcat服务上。

如果有多个服务且域名不同的请求,则在server外加入另外的配置文件,如include vhost/*.conf;则会加载vhost目录下所有conf文件: 以加入pwiki服务为例local.conf: server {

listen 80;

server_name xx.xxx.top;

#charset koi8-r;

#access_log logs/host.access.log main;

#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; }

location / {

root html;

index index.php; }

# 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转发 fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

4.nginx命令

nginx 启动、停止、重启命令

nginx启动

sudo /usr/local/nginx/nginx (nginx二进制文件绝对路径,可以根据自己安装路径实际决定)

nginx从容停止命令,等所有请求结束后关闭服务 ps -ef |grep nginx kill -QUIT nginx主进程号

nginx 快速停止命令,立刻关闭nginx进程 ps -ef |grep nginx kill -TERM nginx主进程号

如果以上命令不管用,可以强制停止 kill -9 nginx主进程号

重新加载配置

修改了配置需要重新加载不需要停止再重启,直接nginx -s reload重新加载。

5.nginx其他功能:

1)加载静态资源,如js、css、图片等 在server中配置location路径

location ~ ^/static/ { root html; }

所有以static开头的请求会重定向到html路径下,寻找static目录对应资源,减轻后端服务器压力,注意html为nginx根目录文件且static内所有文件必须通过chmod 755 -R static进行设置以支持访问权限,否则会出现403/404等错误。

2)负载均衡

nginx可以作为代理服务器,支持负载均衡功能,一般都需要向上游服务器的集群转发请求,这里的负载均衡是指选择一种策略,尽量把请求平均地分布到每一台上游服务器上。 考虑到负载均衡以及会话保持的问题,建议如下配置:

1. upstream tomcat { 2. ip_hash; 3. server app1.xx.net; 4. server app2.xx.net; 5. server app3.xx.net; 6. server app4.xx.net; 7. } 8. 9. location / { 10. proxy_pass http://tomcat;

11. proxy_set_header Host $host;

12. proxy_set_header X-Real-IP $remote_addr;

13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 14. proxy_set_header Cookie $http_cookie; } 15. } 注意:tomcat为upstream的名字,与location中proxy_pass对应,如proxy_pass:http://tomcat,如选择的是第一个server则访问http:// app1.xx.net。

通过ip_hash将同一ip地址请求转到同一台服务器上,避免再没有会话服务器时候出现会话不一致的情况,另外还可以指定权重将请求按权重转发到不同的服务器上,但是权重不能和ip_hash同时使用。其他主要参数如下:

? weight=number:设置向这台上游服务器转发的权重,默认为1。

? max_fails=number:该选项与fail_timeout配合使用,指在fail_timeout时间段内,如果

向当前的上游服务器转发失败次数超过number,则认为在当前的fail_timeout时间段内这台上游服务器不可用。max_fails默认为1,如果设置为0,则表示不检查失败次数。 ? fail_timeout=time:fail_timeout表示该时间段内转发失败多少次后就认为上游服务器暂

时不可用,用于优化反向代理功能。它与向上游服务器建立连接的超时时间,读取上游服务器的响应超时时间等完全无关。fail_timeout默认为10秒。

? down:表示所在的上游服务器永久下线,只在使用ip_hash配置项时才有用。

? backup:在使用ip_hash配置项时它是无效的。它表示所在的上游服务器只是备份服务

器,只有在所有的非备份上游服务器都失效后,才会向所在的上游服务器转发请求。

另外nginx还可以通过rewrite重写url请求,本次没用到,随后在学习。 附:

1.nginx.conf配置详细说明: #运行用户 user nobody;

#启动进程,通常设置成和cpu的数量相等 worker_processes 1;

#全局错误日志及PID文件 #error_log logs/error.log;

#error_log logs/error.log notice; #error_log logs/error.log info;

#pid logs/nginx.pid;

#工作模式及连接数上限 events {

#epoll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高nginx的性能 use epoll;

#单个后台worker process进程的最大并发链接数 worker_connections 1024;

# 并发总数是 worker_processes 和 worker_connections 的乘积 # 即 max_clients = worker_processes * worker_connections # 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么

# 为什么上面反向代理要除以4,应该说是一个经验值

# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000 # worker_connections 值的设置跟物理内存大小有关

# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数

# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右

# 我们来看看360M内存的VPS可以打开的文件句柄数是多少: # $ cat /proc/sys/fs/file-max

# 输出 34336

# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内 # 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置

# 使得并发总数小于操作系统可以打开的最大文件数目 # 其实质也就是根据主机的物理CPU和内存进行配置

# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。

# ulimit -SHn 65535 } http {

#设定mime类型,类型由mime.type文件定义 include mime.types;

default_type application/octet-stream; #设定日志格式

log_format main '$remote_addr - $remote_user [$time_local] \ '$status $body_bytes_sent \ '\

access_log logs/access.log main;

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件, #对于普通应用,必须设为 on,

#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,

#以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on;

#tcp_nopush on;

#连接超时时间

#keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on;

#开启gzip压缩 gzip on;

gzip_disable \

#设定请求缓冲

client_header_buffer_size 128k; large_client_header_buffers 4 128k;

#设定虚拟主机配置 server { #侦听80端口 listen 80;

#定义使用 www.nginx.cn访问 server_name www.nginx.cn;

#定义服务器的默认网站根目录位置 root html;

#设定本虚拟主机的访问日志

access_log logs/nginx.access.log main;

#默认请求 location / {

#定义首页索引文件的名称

index index.php index.html index.htm; }

# 定义错误提示页面

error_page 500 502 503 504 /50x.html; location = /50x.html { }

#静态文件,nginx自己处理

location ~ ^/(images|javascript|js|css|flash|media|static)/ {

#过期30天,静态文件不怎么更新,过期可以设大一点, #如果频繁更新,则可以设置得小一点。 expires 30d; }

#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. location ~ .php$ {

fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }

#禁止访问 .htxxx 文件 location ~ /.ht { deny all; } } }

2.location总结及rewrite规则写法

location正则写法

一个示例:

location = / {

# 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] }

location / {

# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 # 但是正则和最长字符串会优先匹配 [ configuration B ] }

location /documents/ {

# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration C ] }

location ~ /documents/Abc {

# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索 # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条 [ configuration CC ] }

location ^~ /images/ {

# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。

[ configuration D ]

}

location ~* \\.(gif|jpg|jpeg)$ { # 匹配所有以 gif,jpg或jpeg 结尾的请求

# 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则

[ configuration E ] }

location /images/ {

# 字符匹配到 /images/,继续往下,会发现 ^~ 存在 [ configuration F ] }

location /images/abc {

# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在 # F与G的放置顺序是没有关系的 [ configuration G ] }

location ~ /images/abc/ {

本文来源:https://www.bwwdw.com/article/036v.html

Top