手记系列之三 ----- 关于使用Nginx的一些使用方法和经验( 三 )

备注:需要注意的是客户端IP使用 $binary_remote_addr 变量,而不是 \(remote_addr 。\)remote_addr 是一个文本型变量,长度为7至15个字节之间 。$binary_remote_addr 是二进制的IP表达形式,对于IPv4地址,变量的大小始终为4个字节,对于IPv6地址,变量的大小始终为16个字节 。存储状态在32位平台上总是占用32或64字节,在64位平台上占用64字节 。1M的内存空间可以保留大约32000个32字节状态或16000个64位字节 。例子中的10M的话换算下来可以保留16万个IP地址信息 。当超出请求是服务器将返回错误 。
Nginx监听多端口以及Https多端口,就是两端server
https则需要配置证书
server {#listen8181;#server_name192.168.0.1#charset koi8-r;#access_loglogs/host.access.logmain;#rewrite ^(.*)$ https://$host$1 permanent;}# HTTPS server#server {listen8080 ssl;server_name192.168.0.1 ;ssl_certificate/usr/local/nginx/cert/xxx.crt;ssl_certificate_key/usr/local/nginx/cert/xxx.key;ssl_session_cacheshared:SSL:1m;ssl_session_timeout30m;#ssl_ciphersHIGH:!aNULL:!MD5;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_prefer_server_cipherson;location / {root/home/release/dist;indexindex.html index.htm;}location ^~ /api/ {proxy_pass http://127.0.0.1:9999;proxy_cookie_path / /;proxy_pass_header Set-Cookie;proxy_set_header Host zans;}proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}# HTTPS server#server {listen443 ssl;server_namexxx.com ;ssl_certificate/usr/local/nginx/cert/xxx.crt;ssl_certificate_key/usr/local/nginx/cert/xxx.key;ssl_session_cacheshared:SSL:1m;ssl_session_timeout30m;#ssl_ciphersHIGH:!aNULL:!MD5;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_prefer_server_cipherson;location / {root/home/release/app;indexindex.html index.htm;}location ^~ /api {proxy_pass http://127.0.0.1:6666;proxy_cookie_path / /;proxy_pass_header Set-Cookie;proxy_set_header Host zans;}proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}错误处理1,Nginx: error while loading shared libraries: libpcre.so.1
则说明未安装pcre或安装了未设置软链,安装或者设置器软链即可
ln -s /usr/local/lib/libpcre.so.1 /lib64/2,实现负载均衡之后,页面无法跳转
原因: nginx 默认的端口是80,更改监听的端口之后,也需要更改一下 。
也会出现这种错误:net::ERR_NAME_NOT_RESOLVED
解决办法:在location 下添加proxy_set_header Host$host:port 这个配置,port 和listen 的端口保持一致, 不然是无法跳转的 。
3,使用文件服务器的一些问题

  1. nginx需要设置传输的大小;
  2. 访问若出现了403权限问题,一般情况下是上传的图片用root创建的,但nginx访问使用nginx用户访问的,所以在linux系统出现了问题 。解决办法一:nginx使用root用户启动,在nginx的配置改成 use root;解决办法二:全局设置文件夹的权限,并且使nginx拥有读取的权限;
  3. nginx限速问题,如果宽带不高,可以对大图片文件进行限速 。
其他之前写的nginx

经验总结扩展阅读