相关内容原文地址:

简书:joyitsai:Nginx基本功能及其原理

博客园:得你归去来:nginx 配置管理



一、正向代理、反向代理

  1. 正向代理:

    正向代理的过程,它隐藏了真实的请求客户端,服务端不知道真实的客户端是谁,客户端请求的服务都被代理服务器代替来

    请求,某些科学上网工具扮演的就是典型的正向代理角色。用浏览器访问google时,被残忍的block,于是你可以在国外搭建一台代理服务器,让代理帮我去请求google,代理把请求返回的相应结构再返回给我。

  1. 反向代理:

    反向代理隐藏了真实的服务端,当我们请求ww.baidu.com 的时候,就像拨打10086一样,背后可能有成千上万台服务器为我们服务,但具体是哪一台,你不知道,也不需要知道,你只需要知道反向代理服务器是谁就好了,www.baidu.com 就是我们的反向代理服务器,反向代理服务器会帮我们把请求转发到真实的服务器那里去。Nginx就是性能非常好的反向代理服务器,用来做负载均衡。

两者的区别在于代理的对象不一样: 正向代理是为客户端代理,反向代理是为服务端代理

nginx能实现负载均衡,什么是负载均衡呢?就是我的项目部署在不同的服务器上,但是通过统一的域名进入,nginx则对请求进行分发,减轻了服务器的压力。

在上面这两种情况下,nginx服务器的作用都只是作为分发服务器,真正的内容,我们可以放在其他的服务器上,这样来,还能起到一层安全隔壁的作用,nginx作为隔离层

其次,nginx还能解决跨域的问题

二、Nginx配置文件的整体结构

三、Nginx配置SSL及HTTP跳转到HTTPS

# Settings for a TLS enabled server.

# 如果是http请求默认访问80端口,此时return强行301重定向到https://www.joyitsai.com

server {

  listen 80;

  server_name www.joyitsai.com;

  return 301 https://www.joyitsai.com$request_uri;

  # 把http重定向到https使用了nginx的重定向命令,之前老版本的nginx可能使用了以下类似的格式:
# rewrite ^/(.*)$ http://www.joyitsai.com/$1 permanent;
# 或者:
# rewrite ^ http://www.joyitsai.com$request_uri? permanent;
# 现在nginx新版本已经换了种写法,上面这些已经不再推荐。现在网上可能还有很多文章写的是第一种。
# 新的写法比较推荐方式是:return 301 https://www.joyitsai.com$request_uri;
} server { listen 443;
server_name www.joyitsai.com;
root /data/release/weapp/uploadFiles; # 开启ssl功能
ssl on; # 配置ssl证书,直接用.pem和.key文件的绝对路径 ssl_certificate/data/release/nginx/1535530361992.pem; ssl_certificate_key/data/release/nginx/1535530361992.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE - RSA - AES128 - GCM - SHA256: ECDHE: ECDH: AES: HIGH: !NULL: !aNULL: !MD5: !ADH: !RC4; ssl_prefer_server_ciphers on; location / { proxy_pass http://app_weapp; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } location /images/ {
autoindex on;
} # 配置uri, ~用于正则uri前,其中.(png|jpg)为正则表达式,如果后缀是.png或.jpg的url请求,则匹配成功
# root用于配置接收到请求以后查找资源的根目录路径 location ~ \.(png|jpg) {
root /data/release/weapp/uploadFiles;
} error_page 404 /404.html; location = /40x.html {
} error_page 500 502 503 504 /50x.html; location = /50x.html {
}
}

四、nginx 配置管理

博客园:得你归去来:nginx 配置管理

nginx.conf,作为最外层的配置文件,主要设置一些基础的配置就好了,如内存配置,日志格式配置,线程配置等,最后使用一个include conf.d/* 将其他配置文件包含进来即可。

【nginx.conf 基础配置】

user  nginx;
worker_processes auto; error_log /data/var/log/nginx/error.log debug;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} # load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#} http {
include mime.types;
default_type application/octet-stream;
autoindex off;
server_tokens off; server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 20m;
client_body_buffer_size 256k; sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on; fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 32 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k; gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_comp_level 2;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript; log_format main '$request_time $upstream_response_time $remote_addr - $upstream_addr [$time_local] '
'"$host" "$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio" "$http_x_forwarded_for" - "$server_addr" '; access_log /data/var/log/nginx/access.log main; include conf.d/*.conf; }

【conf.d/*, 具体的域名配置,http://】

upstream 3ctest_x123_com {
server 192.168.1.103:81;
keepalive 8;
}
upstream mytest_x123_com {
server 192.168.1.103:80;
keepalive 8;
}
upstream 3capi_x123_com {
server 192.168.1.103:9002;
keepalive 8;
}
upstream yhapi_x123_com {
server 192.168.1.103:8089;
keepalive 8;
} server {
listen 80;
server_name 3ctest.x123.com;
location / {
proxy_pass http://3ctest_x123_com;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
} server {
listen 80;
server_name mytest.x123.com;
location / {
proxy_pass http://mytest_x123_com;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
} server {
listen 80;
server_name 3capi.x123.com;
location / {
proxy_pass http://3capi_x123_com;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
} server {
listen 80;
server_name yhapi.x123.com;
location / {
proxy_pass http://yhapi_x123_com;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
} server {
listen 80;
server_name 192.168.1.22;
location / {
proxy_pass http://192.168.1.22;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
}
} upstream 192.168.1.22 {
server 192.168.1.22:88;
keepalive 8;
}

【Https:// 配置】

server {
listen 443 ssl;
server_name wx.mysite1.com;
ssl on;
ssl_certificate /etc/nginx/conf.d/ssl/mysite1.crt;
ssl_certificate_key /etc/nginx/conf.d/ssl/mysite1.key;
ssl_session_cache shared:SSL:200m;
ssl_session_timeout 20m; ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4; location / {
#proxy_set_header Host $http_host; #proxy_set_header Host $http_host;
#proxy_set_header X-Forwarded-For $remote_addr; # online
#proxy_redirect http://192.168.1.22/ http://$http_host/;
#proxy_pass http://shmc.mysite1.com; #proxy_pass http://192.168.1.22/;
#index index.html;
#root /data/www/; # if ( $cookie_COOKIE ~* "(.*)$") {
# set $all_cookie $1;
# }
# proxy_set_header Cookie "$http_cookie; node_id=018"; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
add_header Access-Control-Allow-Origin *;
proxy_pass http://192.168.1.22;
proxy_redirect off;
}
} server {
listen 80;
server_name wx.mysite1.com;
location / {
proxy_set_header Host $host;
# online
#proxy_redirect http://192.168.1.22/ http://$http_host/;
#proxy_pass http://shmc.mysite1.com;
proxy_pass http://192.168.1.22/;
#index index.html;
#root /data/www/;
}
}

Nginx基本功能及其原理,配置原理的更多相关文章

  1. lvs、nginx、HAProxy、keepalive工作原理

    1. lvs.nginx.HAProxy.keepalive工作原理 1.1. 前言 遇到了负载均衡和高可用选型问题,我觉的有必要好好理解下lvs,nginx,haproxy和keepalive的区别 ...

  2. nginx支持websocket及websocket部分原理介绍

    nginx支持websocket及websocket部分原理介绍最近ipc通过websocket与server进行通行,经过无法通过nginx进行反向代理,只有直连nodejs端口.而且部署到阿里云用 ...

  3. spring boot实战(第十三篇)自动配置原理分析

    前言 spring Boot中引入了自动配置,让开发者利用起来更加的简便.快捷,本篇讲利用RabbitMQ的自动配置为例讲分析下Spring Boot中的自动配置原理. 在上一篇末尾讲述了Spring ...

  4. SpringBoot自动配置原理

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾前面Spring的文章(以学习的顺序排好): S ...

  5. Springboot 系列(三)Spring Boot 自动配置原理

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 关于配置文件可以配置的内容,在 Spring ...

  6. 3. SpringBoot ——自动配置原理浅析

    SpringBoot的功能之所以强大,离不开它的自动配置这一大特色.但估计很多人只是知其然而不知其所以然.下面本人对自动配置原理做一个分析: 在使用SpringBoot时我们通过引入不同的Starte ...

  7. spring boot 自动配置原理

    1).spring boot启动的时候加载主配置类,开启了自动配置功能@EnableAutoConfiguration,先看一下启动类的main方法 public ConfigurableApplic ...

  8. Spring Boot 自动配置原理(精髓)

    一.自动配置原理(掌握) SpringBoot启动项目会加载主配置类@SpringBootApplication,开启@EnableAutoConfiguration自动配置功能 @EnableAut ...

  9. 5. SprigBoot自动配置原理

      配置文件到底能写什么?怎么写? 都可以在SpringBoot的官方文档中找到: 配置文件能配置的属性参照   1.自动配置原理: 1).SpringBoot启动的时候加载主配置类,开启了自动配置功 ...

随机推荐

  1. JavaDailyReports10_19

    今日学习超链接 1.文本链接 使用一对<a>标签 格式:< href ="目标URL" target="目标窗口"> 指针文本    & ...

  2. 什么是urlencode编码

    今天看文章中看到了urlencode,不理解 ,故上网查了查,看到了如下的答案,在此记录下,以加深印象 urlencode编码:就是将字符串以URL编码,一种编码方式,主要为了解决url中中文乱码问题 ...

  3. 如何在Spring Boot项目中集成微信支付V3

    Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 演示例子: paym ...

  4. centos7.5安装Oracle11gR2

    centos7.5安装Oracle11gR2 说明:由于上一台旧的笔记本电脑(CPU:i5-7200,内存:8G,硬盘:128SSD+1T机械)卸任,所以打算在家搭建一个个人服务器(主要是换不锈钢盆不 ...

  5. 域名解析 看Cname 信息

    CMD 命令: nslookup -q=cname www.yuzhentan.com

  6. Mac使用HomeBrew

    前言 考虑许久终于决定入手mac耍耍,还是因为要找工作了,手上的win本大学入的,现在使用卡顿太多,另外就是mac作为程序员之友仰慕已久.决定在PDD入了.到手后发现mac真的跟win有很大差别.还是 ...

  7. 工具用的好,下班回家早!5分钟玩转iTerm2!

    同时打开多个终端窗口,来回切换太麻烦! 能不能像IDEA一样,能够查看历史粘贴记录? 有没有办法一键登陆服务器? 工欲善其事,必先利其器!无论工作还是学习,选择好用的工具真的太重要了.今天就给大家介绍 ...

  8. 【MyBatis】MyBatis 延迟加载策略

    MyBatis 延迟加载策略 文章源码 什么是延迟加载 延迟加载,就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据,也被成为懒加载. 好处:先从单表查询,需要时再从关联表去关联查询,大大提 ...

  9. 常用的N个网站建议收藏

    类型网站路径学习资源及博客论坛网站 书栈网:https://www.bookstack.cn 52 download: http://www.52download.cn/wpcourse/ 菜鸟教程: ...

  10. CVE-2020-0796复现

    今天整理资料时发现了之前存的一个cve漏洞复现过程,当时打算跟着复现来着,后来也没去复现,今天刚好有时间,所以来复现一下这个漏洞 漏洞讲解 https://www.freebuf.com/vuls/2 ...