nginx学习总结

1. 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       8089;
        server_name  localhost;


        rewrite ^/message2/api/(.*) /message/api/$1 last;
        rewrite ^/public/api/(.*) /message/api/$1 last;
         #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /baidu {
          #  root   html;
          #  index  index.html index.htm;
         #  proxy_pass  http://testmessage.shishike.com;
           proxy_pass  http://www.baidu.com;
        } 

         location /baidu2 {
          #  root   html;
          #  index  index.html index.htm;
         #  proxy_pass  http://testmessage.shishike.com;
           proxy_pass  http://www.baidu.com;
        }

        location / {
          #  root   html;
          #  index  index.html index.htm;
           proxy_pass  http://testmessage.shishike.com;
         #   proxy_pass  http://www.baidu.com;
        }

          location ~* ^/message/ {
          #  root   html;
          #  index  index.html index.htm;
          #  proxy_pass  http://172.16.30.55:10789;

          #    eth        off;
              proxy_connect_timeout 30;
              proxy_read_timeout 180;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_pass http://127.0.0.1:10789;
        } 

        #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_certificate      fd.crt;
        ssl_certificate_key   fd.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;
        }
    }

}



127.0.0.1 - - [05/Apr/2018:21:08:44 +0800] "POST /public/api/recharge/v1/queryBalance HTTP/1.1" 502 541 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
127.0.0.1 - - [05/Apr/2018:21:12:11 +0800] "POST /public/api/recharge/v1/queryBalance HTTP/1.1" 200 674 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
2.自定义编译install
  1. 本人是用的mac来安装nginx,采用source来自己编译安装,configure,make,make install

  2. nginx 需要依赖pcre,openssl两个模块,需要自己安装。我的mac电脑已使用brew来安装这两个模块。

brew install pcre openssl

1.    CRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库,在《》 Rewrite规则与实例中提到。
2.   Openssl  开源的加密库tools
  1. 3.
./configure  --prefix=/Users/andrexu/devsoft/nginx\
    --with-http_ssl_module\
    --with-pcre="/usr/local/opt/pcre/include/"\
    --with-openssl="/usr/local/opt/openssl/include/"
  • configure check通过。
Configuration summary
  + using PCRE library: /usr/local/opt/pcre/include/
  + using OpenSSL library: /usr/local/opt/openssl/include/
  + using system zlib library

  nginx path prefix: "/Users/andrexu/devsoft/nginx"
  nginx binary file: "/Users/andrexu/devsoft/nginx/sbin/nginx"
  nginx modules path: "/Users/andrexu/devsoft/nginx/modules"
  nginx configuration prefix: "/Users/andrexu/devsoft/nginx/conf"
  nginx configuration file: "/Users/andrexu/devsoft/nginx/conf/nginx.conf"
  nginx pid file: "/Users/andrexu/devsoft/nginx/logs/nginx.pid"
  nginx error log file: "/Users/andrexu/devsoft/nginx/logs/error.log"
  nginx http access log file: "/Users/andrexu/devsoft/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

  ```
- make 编译的时候报错。仔细检查发现with-pcre是用来指定源码libaray的库。一定不要在网上随便copy,结合官方文档理解明白configure的意思。

./configure –prefix=/Users/andrexu/devsoft/nginx\
–with-http_ssl_module\
–with-cc-opt=”-I/usr/local/opt/openssl/include/ -I/usr/local/opt/pcre/include/”\
–with-ld-opt=”-L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/”

参考文档:https://www.cnblogs.com/taiyonghai/p/6728707.html
#### 3. openssl与nginx地配置

1. openssl的证书生成

- private key
- 数字签名
- 证书

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

 ssl_certificate      fd.crt;
 ssl_certificate_key   fd.key;

“`

  1. 参考
    https://blog.csdn.net/u014497502/article/details/51504412

https://blog.csdn.net/u012198553/article/details/78734272
2. 错误

nginx: [emerg] bind() to 0.0.0.0:443 failed (13: Permission denied)

如遇到如上错误,请以root 启动或者sudo。

3.

#

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值