栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx在Linux的安装和简单使用

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx在Linux的安装和简单使用

文章目录
  • 安装
    • 下载安装包和依赖
      • 安装
    • nginx的配置文件
    • 反向代理配置
    • nginx 配置实例-负载均衡
      • Nginx 提供了几种分配方式(策略):
    • nginx 配置实例-动静分离
    • 反向代理和静态服务器的配置规律

安装 下载安装包和依赖

浏览器中输入http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz即可下载
下载nginx的安装包
http://nginx.org/en/download.html

安装
  1. tar –xvf pcre-8.37.tar.gz 解压压缩文件
  2. 进入pcre-8.37目录执行./configure命令
  3. 安装 openssl 、zlib 、 gcc 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
  1. 安装 nginx
    使用命令解压nginx安装包并进入nginx安装包目录
  2. 执行./configure 命令后,执行make && make install命令
  3. 进入/usr/local/nginx/sbin 目录启动nginx,使用./nginx命令
  4. 通过http://ip/访问,可访问到说明可以启动成功
  5. 关闭nginx使用./nginx -s stop命令
  6. 重新加载nginx使用./nginx -s reload命令
nginx的配置文件

cd /usr/local/nginx/conf/ 路径下的nginx.conf文件
配置文件中的内容
包含三部分内容

  • 全局块:配置服务器整体运行的配置指令
    比如 worker_processes 1;处理并发数的配置
  • events 块:影响 Nginx 服务器与用户的网络连接
    比如 worker_connections 1024; 支持的最大连接数为 1024
  • http 块
    还包含两部分:
    http 全局块
    server 块
反向代理配置
  1. 监听端口,如果监听到就转向另外的端口
    监听80端口,转向8080端口
    访问http://192.168.64.123:80将请求转发到http://192.168.64.123:8080/,配置中的proxy_pass为代理地址
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;
    #}
}
  1. 根据访问的路径跳转到不同端口的服务中
    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 提供了几种分配方式(策略):
  1. 轮询(默认)
    每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除
  2. weight
    weight 代表权,重默认为 1,权重越高被分配的客户端越多
    指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况 ,如
upstream server_pool{
	server 192.168.5.21 weight=10;
	server 192.168.5.22 weight=10;
}
  1. ip_hash
    每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。 例如:
upstream server_pool{
	ip_hash;
	server 192.168.5.21:80;
	server 192.168.5.22:80;
}
  1. 、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路径下的图片

反向代理和静态服务器的配置规律

可以把配置中的location后面的匹配路径作为分隔符,匹配到了后将请求路径中分隔符前面的部分替换为root 或者proxy_pass后面的配置参数生成一个新的路径即为代理路径或者静态页面的路径

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/451178.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号