Nginx 使用教程

1.Nginx 是什么

Nginx 是一个用C语言写的http以及反向代理 web服务器.

名词解释(正向,反向)

正向以及反向,主要判断方式就是

浏览器是否知道自己访问的是代理服务器.

正向: 浏览器设置代理服务器,然后把请求发送给代理请求服务器,然后代理服务器发送给目标服务器,代理接受目标服务器响应之后,解析完,再把结果返归给浏览器

反向:浏览器直接发起请求,代理服务器接收后,发给目标服务器,然后返回结果给代理,代理再把解析后结果发送给浏览器.

使用场景

1.反向代理、2.负载均衡、3.动静分离

2.Nginx 如何使用

命令:

./nginx 启动

./nginx -s reload 重新加载conf文件

./nginx -s stop 关闭nginx

配置:

conf配置一共分三个部分:全局 ,event, http

以下是一个conf demo例子示范配置:

负载均衡配置例子: /tomcatServer

反向代理配置例子: /test

动静分离(静态资源)配置例子: /static

#第一部分 大块配置:全局区
#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;
​
#第二大部分大块配置:event区
events {
# 支持单个worker进程最大的并发数
    worker_connections  1024;
}
​
#第三部分大块配置,最主要的配置:http区
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;
    # 使用代理,负载均衡
   upstream tomcatServer {
       consistent_hash $request_uri;
       server localhost:8081;
       server localhost:8082;
    }
    # 需要启动服务的配置
    server {
        #监听80端口
        listen       80;
        #监听 www.luacasnginx.com ip
        server_name  www.lucasnginx.com;
​
        #charset koi8-r;
​
        #access_log  logs/host.access.log  main;
        
        #匹配相对路径
        location / {
          root   html;
           index  index.html index.htm;
        }
        
        # 配置静态资源路径,在服务器的static文件夹下,alias与root有区别.下面会区分
        location ^~/static {
                alias   /static/;
                autoindex on;
        }
    
        #配置普通路径,若访问www.lucasnginx.com/tomcat 即反向代理去访问上面upstream的 #tomcatserver
        location /tomcat {
        proxy_pass http://tomcatServer/ ;
        }
        
        location /loginProject {
        proxy_pass http://tomcatServer/LoginProject/;
        }
        
        location /test {
            proxy_pass http://127.0.0.1:8080;
        }
​
        #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;
        }
    }
}
​

注意点

静态资源配置:

ROOT 与alias 区别.用上面的 例子去说明

若使用root而不是alias的话.配置如下

 location ^~/static {
                root   /static/;
                autoindex on;
      }

在浏览器访问的话,http://www.lucasnginx.com/static/login.html,表示想访问服务器上static文件夹里面的login.html文件;访问后会发现404。因为使用root的话,匹配了的url也会变成你要访问的路径内容,即 /static/static/login.html(实际是nginx自己解析出来的路径),但实际上我想用url识别到static后,把后面的url才当做具体要访问的路径,所以用alias

浏览器URL关键字实际访问服务器路径
http://www.lucasnginx.com/static/login.htmlroot/static/static/login.html
http://www.lucasnginx.com/static/login.htmlalias/static/login.html

 

### Nginx 使用教程 #### 安装配置示例 对于 Windows 用户而言,安装和配置 Nginx 可以按照如下方式进行: 在下载阶段,访问官方网站获取最新版本的 Nginx 压缩包[^1]。解压到指定位置即完成所谓的“安装”。为了验证是否安装成功,在命令提示符下进入解压后的目录并通过 `start nginx` 启动服务;通过浏览器访问 `http://localhost/` 若能显示欢迎页面则表明安装无误。 针对 Linux 平台上的特定需求如 Memcached 的集成,则需额外执行一些操作来满足缓存加速的需求。具体来说,先利用 YUM 工具依次安装依赖库 libevent 和 memcached 自身,并设置其运行参数以便于后续与 Nginx 协同工作[^2]。 ```bash yum install libevent -y yum install memcached -y memcached -d -m 128m -p 11211 -l 192.168.32.17 -u root -P /opt/mempid pgrep memcached ``` 关于 Nginx 配置文件的整体结构以及各部分的作用,有详细的分层描述可供参考。全局块用于定义影响整个进程的行为选项;events 块主要处理网络事件相关的设定;而 http 块则是最核心的部分,它包含了多个 server 块,每个 server 块又可进一步细分为若干 location 块用来精确控制不同 URL 请求如何被响应[^4]。 ```nginx # 全局块 user nobody; worker_processes auto; #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; } } } ``` 此段代码展示了最基本的 Web 服务器配置方式——监听 HTTP 默认端口 (80),当接收到请求时会尝试从本地磁盘路径 `/html` 下寻找对应的 HTML 文件作为回应内容发送给客户端浏览设备。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值