nginx的使用

本文详细介绍了如何在Linux系统上更新yum包,安装必要的依赖,如pcre、openssl等,以及如何下载、安装和配置Nginx,包括常用操作命令、配置文件结构和示例,涉及反向代理、负载均衡及动静分离的应用。
摘要由CSDN通过智能技术生成

1、准备工作

更新yum

yum update 
yum install -y gcc gcc-c++

2、nginx 安装

(1)安装 pcre 依赖
# 下载安装包
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
# 解压压缩包
tar –xvf pcre-8.37.tar.gz
# 编译
cd pcre-8.37
./configure
make && make install
# 查看pcre依赖是否安装成功
pcre-config --version

(2)安装 openssl 、zlib 、 gcc 依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
(3) 上传nginx压缩包到服务器,并解压编译
# 解压
tar -xvf nginx-1.24.0.tar.gz
# 进入到解压后的目录
cd nginx-1.24.0
# 检查
./configure
#编译
make && make install

注:安装成功后会在/usr目录下多出一个文件夹local/nginx,在nginx有sbin有启动脚本	

3、nginx的常用操作命令

# 进入到nginx的安装目录
cd /usr/local/nginx/sbin
# 查看nginx版本号
./nginx -v

# 启动nginx
./nginx

#关闭nginx
./nginx -s stop

# 重新加载
./nginx -s reload

4、nginx的配置文件

1、位置

vim /usr/local/nginx/config/nginx.conf

2、nginx配置文件组成
#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;
    #    }
    #}    
    }
1、nginx配置文件有三部分组成

第一部分 全局块:

​ 从配置文件开始到events块之间的内容,主要设置一些影响nginx服务器整体运行配置指令

​ 比如worker_processes 指的是并发处理的数量

第二部分events块

​ events涉及的指令主要影响nginx服务器与用户网络链接

​ 比如worker_connections 1024 支持最大连接数

第三部分

5、nginx使用实例

1、nginx的反向代理
1)、nginx的反向代理(一)
# 实现效果
打开浏览器,在浏览器地址栏中输入地址,www.123.com 跳转到linux系统的tomacat主页面
# 准备工作
1、在linux系统中安装tomcat,使用默认端口号8080

在这里插入图片描述

注: server_name 是ip地址(nginx服务器),listen监听端口号,proxy_pass 代理地址,

2)、nginx的反向代理(二)
# 实现效果
根据不同的路径跳到不同的目标,
    server {
        listen       9001;
        server_name  39.98.41.81;

        location ~ /edu/ {
        				# 这里代理的url默认会把edu给去掉,
                proxy_pass http://39.98.41.81:8081;
                # 如果想带上edu,则用下面这种方式
                # proxy_pass http://39.98.41.81:8081$request_uri;
        }
        location ~ /vod/ {
        				# 这里代理的url默认会把edu给去掉,
                proxy_pass http://39.98.41.81:8080;
        }
    }
3)、反向代理-前后端分离
server {
    listen        9002;
    server_name   39.98.41.81;

    root /usr/local/nginx/html/static/mubai-generator;
    index index.html;

		# 多个location会按照url的路径匹配
    location / {
       # 如果找不到会进入到这里
        try_files $uri $uri/ /index.html;
    }


    # 静态文件的缓存配置
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
            expires 1y;
            add_header Cache-Control "public";
        }

    # 反向代理到后端API的配置(如果需要)
        location /gen/ {
            # 代理后的地址会默认去掉/gen/
            proxy_pass http://ip:port/mubai-generator/gen/;
        }

    }
2、负载均衡

在这里插入图片描述

3、nginx的动静分离

#进入到nginx的安装目录下

cd /usr/local/nginx/html

#创建static/www1和static/www2的目录

mkdir -p static/www1

mkdir -p static/www2

修改nginx.conf文件
在这里插入图片描述

访问地址http://39.98.41.81/www2/b.html

上面配置:/www1/下的路径都会被发送到/usr/local/nginx/html/static/www1下

autoindex表示开启目录,在访问时会显示文件的目录结构

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值