LingYanSi / blog

博客
https://github.com/LingYanSi/blog/issues
9 stars 0 forks source link

nginx配置 #18

Open LingYanSi opened 8 years ago

LingYanSi commented 8 years ago

nginx 常规配置

#user  nobody;
# 如果对于静态资源站出现403 forbidden 可以使用 user root; 解决
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
    gzip  on; 

    #指定可以使用的gzip文件类型 nginx默认只支持html/text
    gzip_types       text/plain text/css application/x-javascript application/javascript application/xml;

    # 最大文件
    client_max_body_size 5m;

    #禁止IP访问  
    server  {  
      listen 80 default;  
      server_name _;  
      return 444;  
   } 

    # server配置文件
    include server/*.conf; 
}
LingYanSi commented 8 years ago
upstream fuck {
    server localhost:3000 ;
}

server {
    listen       7777;
    server_name  www.shabi.com;

    # 直接返回文件内容
    location = /filename.ext {
         return 200 "可以处理微信js安全域名校验之类的事情";   
    }

    # nginx location其实类似于其他语言的if else,进入逻辑后就不会进入其他逻辑
    # 以xx结尾的请求
    location ~ \.(js|css|html|jpg|png|webp|gif|mp4|mp3)$ {
        #指向一个目录
        root html;
        # 过期日期
        expires 365d;
        # 自动找到文件
        autoindex on;
    }
    # pathname = /about的
    location = /about {
        # 添加头信息
        add_header  Content-Type 'text/html; charset=utf-8';
        add_header  sb 'fuck you';
        # 过期时间
        # expires 10d;
        return 200 '关于';
    }
    location = /json {
        # 添加头信息
        add_header  Content-Type 'text/json; charset=utf-8';
        return 200 '{"code": 1001}';
    }

    location = /redirect {
        return 301 http://baidu.com;
    }

    #pathname 以 /blog开头的的路径
    location /blog {
        # 会把请求代理转发到另一个服务器
        proxy_pass  http://fuck;
    }

   # 其他所有请求
   location  / {
        # 会把请求代理转发到另一个服务器
        proxy_pass  http://fuck;
    }

    location /good {
        # https://stackoverflow.com/questions/33989060/how-to-serve-a-directory-of-static-files-at-a-certain-location-path-with-nginx
        alias /root/project;
        index index.html;
    }
}
LingYanSi commented 7 years ago

proxy_pass add_header

请求转发后,默认add_header是无效的 可以使用这个包headers-more-nginx-module

LingYanSi commented 7 years ago

cors-nginx.cnf

#
# Slightly tighter CORS config for nginx
#
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs
#
# Despite the W3C guidance suggesting that a list of origins can be passed as part of
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox) 
# don't seem to play nicely with this.
#
# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting 
# method to control access instead.
#
# NB: This relies on the use of the 'Origin' HTTP Header.

location / {

    if ($http_origin ~* (whitelist\.address\.one|whitelist\.address\.two)) {
        set $cors "true";
    }

    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
    # Determine the HTTP request method used

    if ($request_method = 'OPTIONS') {
        set $cors "${cors}options";  
    }
    if ($request_method = 'GET') {
        set $cors "${cors}get";  
    }
    if ($request_method = 'POST') {
        set $cors "${cors}post";
    }

    if ($cors = "true") {
        # Catch all incase there's a request method we're not dealing with properly
        add_header 'Access-Control-Allow-Origin' "$http_origin";
    }

    if ($cors = "trueget") {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    }

    if ($cors = "trueoptions") {
        add_header 'Access-Control-Allow-Origin' "$http_origin";

        #
        # Om nom nom cookies
        #

        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #

        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

        #
        # Tell client that this pre-flight info is valid for 20 days
        #

        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }

    if ($cors = "truepost") {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

    }

}
LingYanSi commented 7 years ago

https配置

server {
        # https请求默认端口
        listen       443;
        # 指定域名
        server_name  www.lingyansi.space;
        client_max_body_size 20M; # 允许最大请求

        # 指定证书
        ssl_certificate      /root/.acme.sh/fake.lingyansi.space/fake.lingyansi.space.cer;
        ssl_certificate_key   /root/.acme.sh/fake.lingyansi.space/fake.lingyansi.space.key;

        # ssl开启
        ssl on;

        ssl_protocols TLSv1;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
           # 单纯转发会丢失客户端ip,3000端口上的服务获取到的将是nginx的ip地址也就是127.0.0.1
           # 通过proxy_set_header把真正的ip通过head传递到3000服务上
           # 服务端通过header['X-Forwarded-For'] 获取ip
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass  http://127.0.0.1:3000; 
        }
    }

server {
        # http请求默认80端口
        listen 80;
        server_name lingyansi.space www.lingyansi.space;
        client_max_body_size 20M; # 允许最大请求

        location / {
            # 跳转到https
            return 301 https://lingyansi.space;
            # proxy_pass  http://127.0.0.1:3000;
        }
}

使用ace 获取免费证书

ace

LingYanSi commented 7 years ago

三方modules

LingYanSi commented 7 years ago

module介绍

看这个项目

/*
--with-aio_module 使用AIO方式处理事件驱动
注意:这里的aio module只能与FreeBSD操作系统上的kqueue事件处理机制合作,Linux上无法使用
默认情况下是不安装aio module的
(2)默认即编译进入Nginx的HTTP模块
表1-10列出了默认就会编译进Nginx的核心HTTP模块,以及如何把这些HTTP模块从产品中去除。
表1-10 configure中默认编译到Nginx中的HTTP模块参数
默认安装的HTTP模块                        意 义
--without-http_charset_module 不安装http charset module。这个模块可以将服务器发出的HTTP响应重编码
--without-http_gzip_module 不安装http gzip module。在服务器发出的HTTP响应包中,这个模块可以按照配置文件指定的content-type对特定大小的HTTP响应包体执行gzip压缩
--without-http_ssi_module 不安装http ssi module。该模块可以在向用户返回的HTTP响应包体中加入特定的内容,如HTML文件中固定的页头和页尾
--without-http_userid_module 不安装http userid module。这个模块可以通过HTTP请求头部信息里的一些字段认证用户信息,以确定请求是否合法
--without-http_access_module 不安装http access module。这个模块可以根据IP地址限制能够访问服务器的客户端
--without-http_auth_basic_module 不安装http auth basic module。这个模块可以提供最简单的用户名/密码认证
--without-http_autoindex_module 不安装http autoindex module。该模块提供简单的目录浏览功能
--without-http_geo_module 不安装http geo module。这个模块可以定义一些变量,这些变量的值将与客户端IP地址关联,这样Nginx针对不同的地区的客户端(根据IP地址判断)返回不一样的结果,例如不同地区显示不同语言的网页
--without-http_map_module 不安装http map module。这个模块可以建立一个key/value映射表,不同的key得到相应的value,这样可以针对不同的URL做特殊处理。例如,返回302重定向响应时,可以期望URL不同时返回的Location字段也不一样
--without-http_split_clients_module 不安装http split client module。该模块会根据客户端的信息,例如IP地址、header头、cookie等,来区分处理
--without-http_referer_module 不安装http referer module。该模块可以根据请求中的referer字段来拒绝请求
--without-http_rewrite_module 不安装http rewrite module。该模块提供HTTP请求在Nginx服务内部的重定向功能,依赖PCRE库
--without-http_proxy_module 不安装http proxy module。该模块提供基本的HTTP反向代理功能
--without-http_fastcgi_module 不安装http fastcgi module。该模块提供FastCGI功能
--without-http_uwsgi_module 不安装http uwsgi module。该模块提供uWSGI功能
--without-http_scgi_module 不安装http scgi module。该模块提供SCGI功能
--without-http_memcached_module 不安装http memcached module。该模块可以使得Nginx直接由上游的memcached服务读取数据,并简单地适配成HTTP响应返回给客户端
--without-http_limit_zone_module 不安装http limit zone module。该模块针对某个IP地址限制并发连接数。例如,使Nginx对一个IP地址仅允许一个连接
--without-http_limit_req_module 不安装http limit req module。该模块针对某个IP地址限制并发请求数
--without-http_empty_gif_module 不安装http empty gif module。该模块可以使得Nginx在收到无效请求时,立刻返回内存中的1×1像素的GIF图片。这种好处在于,对于明显的无效请求不会去试图浪费服务器资源
--without-http_browser_module 不安装http browser module。该模块会根据HTTP请求中的user-agent字段(该字段通常由浏览器填写)来识别浏览器
--without-http_upstream_ip_hash_module 不安装http upstream ip hash module。该模块提供当Nginx与后端server建立连接时,会根据IP做散列运算来决定与后端哪台server通信,这样可以实现负载均衡
(3)默认不会编译进入Nginx的HTTP模块
表1-11列出了默认不会编译至Nginx中的HTTP模块以及把它们加入产品中的方法。
表1-11 configure中默认不会编译到Nginx中的HTTP模块参数
可选的HTTP 模块 意 义
--with-http_ssl_module 安装http ssl module。该模块使Nginx支持SSL协议,提供HTTPS服务。
注意:该模块的安装依赖于OpenSSL开源软件,即首先应确保已经在之前的参数中配置了OpenSSL
--with-http_realip_module 安装http realip module。该模块可以从客户端请求里的header信息(如X-Real-IP或者X-Forwarded-For)中获取真正的客户端IP地址
--with-http_addition_module 安装http addtion module。该模块可以在返回客户端的HTTP包体头部或者尾部增加内容
--with-http_xslt_module 安装http xslt module。这个模块可以使XML格式的数据在发给客户端前加入XSL渲染
注意:这个模块依赖于libxml2和libxslt库,安装它前首先确保上述两个软件已经安装
--with-http_image_filter_module 安装http image_filter module。这个模块将符合配置的图片实时压缩为指定大小(width*height)的缩略图再发送给用户,目前支持JPEG、PNG、GIF格式。
注意:这个模块依赖于开源的libgd库,在安装前确保操作系统已经安装了libgd
--with-http_geoip_module 安装http geoip module。该模块可以依据MaxMind GeoIP的IP地址数据库对客户端的IP地址得到实际的地理位置信息
注意:该库依赖于MaxMind GeoIP的库文件,可访问http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz获取
--with-http_sub_module 安装http sub module。该模块可以在Nginx返回客户端的HTTP响应包中将指定的字符串替换为自己需要的字符串
例如,在HTML的返回中,将</head>替换为</head><script language="javascript" src="$script"></script>
--with-http_dav_module 安装http dav module。这个模块可以让Nginx支持Webdav标准,如支持Webdav协议中的PUT、DELETE、COPY、MOVE、MKCOL等请求
--with-http_flv_module 安装http flv module。这个模块可以在向客户端返回响应时,对FLV格式的视频文件在header头做一些处理,使得客户端可以观看、拖动FLV视频
--with-http_mp4_module 安装http mp4 module。该模块使客户端可以观看、拖动MP4视频
--with-http_gzip_static_module 安装http gzip static module。如果采用gzip模块把一些文档进行gzip格式压缩后再返回给客户端,那么对同一个文件每次都会重新压缩,这是比较消耗服务器CPU资源的。gzip static模块可以在做gzip压缩前,先查看相同位置是否有已经做过gzip压缩的.gz文件,如果有,就直接返回。这样就可以预先在服务器上做好文档的压缩,给CPU减负
--with-http_random_index_module 安装http random index module。该模块在客户端访问某个目录时,随机返回该目录下的任意文件
--with-http_secure_link_module 安装http secure link module。该模块提供一种验证请求是否有效的机制。例如,它会验证URL中需要加入的token参数是否属于特定客户端发来的,以及检查时间戳是否过期
--with-http_degradation_module 安装http degradation module。该模块针对一些特殊的系统调用(如sbrk)做一些优化,如直接返回HTTP响应码为204或者444。目前不支持Linux系统
--with-http_stub_status_module 安装http stub status module。该模块可以让运行中的Nginx提供性能统计页面,获取相关的并发连接、请求的信息(14.2.1节中简单介绍了该模块的原理)
--with-google_perftools_module 安装google perftools module。该模块提供Google的性能测试工具
(4)邮件代理服务器相关的mail模块
表1-12列出了把邮件模块编译到产品中的参数。
表1-12 configure提供的邮件模块参数
可选的mail 模块 意 义
--with-mail 安装邮件服务器反向代理模块,使Nginx可以反向代理IMAP、POP3、SMTP等协议。该模块默认不安装
--with-mail_ssl_module 安装mail ssl module。该模块可以使IMAP、POP3、SMTP等协议基于SSL/TLS协议之上使用。该模块默认不安装并依赖于OpenSSL库
--without-mail_pop3_module 不安装mail pop3 module。在使用--with-mail参数后,pop3 module是默认安装的,以使Nginx支持POP3协议
--without-mail_imap_module 不安装mail imap module。在使用--with-mail参数后,imap module是默认安装的,以使Nginx支持IMAP
--without-mail_smtp_module 不安装mail smtp module。在使用--with-mail参数后,smtp module是默认安装的,以使Nginx支持SMTP
*/
LingYanSi commented 7 years ago

三方modules安装

nginx有默认模块和三方模块 默认模块:默认开启、默认不开启模块

对于默认不开启模块需要 --with-default-module-name 对于第三方模块需要 --add-module=/path/to/module

--prefix 表示nginx安装位置

因此

./configure \
--prefix=/usr/local/nginx \
 --with-http_ssl_module \
 --with-http_addition_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--add-module=./../modules/nginx-http-concat/ \
 --add-module=./../modules/echo-nginx-module-0.60
LingYanSi commented 7 years ago

mac安装http_ssl_module报openssl canot find的问题

通过重新安装openssl可以解决

在make install的时候遇到/usr/local/ssl不存在,自己手动创建即可解决

忙了一天才解决这个问题 😅

centos安装的时候也会报pcre错

yum -y install pcre-devel openssl openssl-devel

可结局

LingYanSi commented 7 years ago

nginx 安装位置

ps aux | grep nignx

nginx环境下一般放置在 /etc/nginx

LingYanSi commented 3 years ago

单页应用(SPA),只返回index.html 参考文档

location / {
      root /xx/xxxx;
      index index.html;
      add_header expires 0;
      add_header cache-control no-cache;
      try_files $uri /index.html; # 如果文件不存在,直接返回index.html 
}