zoukankan      html  css  js  c++  java
  • 【nginx】配置详解

    nginx作为web server,是最常用的网络服务器。来看下它的配置文件。

    实验1:最基础的配置(可访问静态资源)

    包括listen,server_name,root,index,access_log, error_log

    server {
        listen 80; 
        server_name lww.demo.com;
        root /home/luwenwei/dev/project/demo.com;
        index index.html;
    
        access_log /data/nginx/logs/lww.access.log;
        error_log /data/nginx/logs/lww.error.log;
    }

    创建项目文件夹:mkdir /home/luwenwei/dev/project/demo.com

    创建文件:index.html

    然后启动nginx:service nginx start

    访问网站:curl -H "Host: lww.demo.com" http://127.0.0.1/index.html,可以看到文件中的内容。

    解释:

    listen:监听端口,默认是80

    server_name:项目的域名

    root:项目根目录

    index:项目默认访问地址,访问: http://server_name/会自动路由到index配置的文件

    access_log和error_log:访问日志和错误日志

    实验2:直接返回http_code和body内容

        location /200 {
            return 200 "I am env
    ";
        }   
    
        location /500 {
            return 500 "I am 500 page
    ";
        }

    再实验1的基础上加上两个配置。

    测试访问:curl -H "Host: lww.demo.com" http://127.0.0.1/200 -v

    输出:

    *   Trying 127.0.0.1...
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > GET /200 HTTP/1.1
    > Host: lww.demo.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Mon, 12 Feb 2018 07:35:22 GMT
    < Content-Type: application/octet-stream
    < Content-Length: 9
    < Connection: keep-alive
    < 
    I am env
    * Connection #0 to host 127.0.0.1 left intact

    可以看到输出的http_code和body内容和我们设置的一致,符合预期。

    访问测试:curl -H "Host: lww.demo.com" http://127.0.0.1/500 -v

    *   Trying 127.0.0.1...
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > GET /500 HTTP/1.1
    > Host: lww.demo.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 500 Internal Server Error
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Mon, 12 Feb 2018 07:36:25 GMT
    < Content-Type: application/octet-stream
    < Content-Length: 14
    < Connection: keep-alive
    < 
    I am 500 page
    * Connection #0 to host 127.0.0.1 left intact

    可以看到输出的内容,http_code=500,和我们设置的一致,符合预期。

    一定要注意这里有location的语法,如果直接全部匹配,使用:location /URI即可。

     实验3:rewrite

        location /304 {
            rewrite /304 /200 last;
        }

    rewrite的语法:rewrite <regex> <replacement> <flag>

    rewrite更多的语法可以参见:https://www.cnblogs.com/czlun/articles/7010604.html

    flat: break, last, redirect, permanent

    测试:

    $ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v
    *   Trying 127.0.0.1...
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > GET /304 HTTP/1.1
    > Host: lww.demo.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Mon, 12 Feb 2018 07:43:27 GMT
    < Content-Type: application/octet-stream
    < Content-Length: 9
    < Connection: keep-alive
    < 
    I am env
    * Connection #0 to host 127.0.0.1 left intact

     接下来看下,不同的<flag>下,同一个访问请求的响应结果。

    <flag=break>
    $ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v
    *   Trying 127.0.0.1...
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > GET /304 HTTP/1.1
    > Host: lww.demo.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 404 Not Found
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Mon, 12 Feb 2018 07:48:08 GMT
    < Content-Type: text/html
    < Content-Length: 178
    < Connection: keep-alive
    < 
    <html>
    <head><title>404 Not Found</title></head>
    <body bgcolor="white">
    <center><h1>404 Not Found</h1></center>
    <hr><center>nginx/1.10.3 (Ubuntu)</center>
    </body>
    </html>
    * Connection #0 to host 127.0.0.1 left intact
    
    <flag=redirect>
    $ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v
    *   Trying 127.0.0.1...
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > GET /304 HTTP/1.1
    > Host: lww.demo.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 302 Moved Temporarily
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Mon, 12 Feb 2018 07:48:49 GMT
    < Content-Type: text/html
    < Content-Length: 170
    < Location: http://lww.demo.com/200
    < Connection: keep-alive
    < 
    <html>
    <head><title>302 Found</title></head>
    <body bgcolor="white">
    <center><h1>302 Found</h1></center>
    <hr><center>nginx/1.10.3 (Ubuntu)</center>
    </body>
    </html>
    * Connection #0 to host 127.0.0.1 left intact
    
    
    <flag=permanent>
    $ curl -H "Host: lww.demo.com" http://127.0.0.1/304 -v
    *   Trying 127.0.0.1...
    * Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
    > GET /304 HTTP/1.1
    > Host: lww.demo.com
    > User-Agent: curl/7.47.0
    > Accept: */*
    > 
    < HTTP/1.1 301 Moved Permanently
    < Server: nginx/1.10.3 (Ubuntu)
    < Date: Mon, 12 Feb 2018 07:49:22 GMT
    < Content-Type: text/html
    < Content-Length: 194
    < Location: http://lww.demo.com/200
    < Connection: keep-alive
    < 
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.10.3 (Ubuntu)</center>
    </body>
    </html>
    * Connection #0 to host 127.0.0.1 left intact

    需要注意的是,redirect和permanent两者结果类似。

    redirect:#返回302临时重定向,以及Location的header,浏览器地址会显示跳转后的URL地址

    permanent:#返回301永久重定向,以及Location的header,浏览器地址栏会显示跳转后的URL地址

  • 相关阅读:
    给你一个长度为 n 的数组,其中只有一个数字出现了大于等于 n/2 次,问如何使用优秀的 时空复杂度快速找到这个数字。
    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现偶数次。找出那个只出现了一次的元素。
    python虚拟环境配置
    测试环境配置
    使用ELK Stack收集kubernetes集群内的应用日志
    vue 禁止遮罩层下的页面滑动
    vue 把 java 传过来的流文件 转成apk、xls等
    vue 中使用 webSocket 收发数据, 增加 " 心跳机制 " 保持连接.
    webstrom 根据当前编辑文件定位左侧目录
    MySQL 8.0新特性详解(转)
  • 原文地址:https://www.cnblogs.com/helww/p/8444694.html
Copyright © 2011-2022 走看看