篇二:Nginx配置说明及实例演示

篇一:https://blog.csdn.net/weixin_43970743/article/details/103460763 

相信根据篇一步骤走,不差的都已经成功安装好了Nginx ,那好,接下来,将介绍一下Nginx最重要的配置说明,以及通过实例介绍通过Nginx如何实现反向代理;

一个人可能走的更快,但是一群人会走的更远!你们说,对吗?

一、nginx.conf 配置说明

#user  nobody;
worker_processes  1; #工作进程:数目。根据硬件调整,通常等于cpu数量或者2倍cpu数量。
#错误日志存放路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid; # nginx进程pid存放路径
events {
    worker_connections  1024; # 工作进程的最大连接数量

http {
    include       mime.types; #指定mime类型,由mime.type来定义
    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; #用log_format指令设置日志格式后,需要用access_log来指定日志文件存放路径
                  
    sendfile        on; #指定nginx是否调用sendfile函数来输出文件,对于普通应用,必须设置on。
            如果用来进行下载等应用磁盘io重负载应用,可设着off,以平衡磁盘与网络io处理速度,降低系统uptime。
    #tcp_nopush     on; #此选项允许或禁止使用socket的TCP_CORK的选项,此选项仅在sendfile的时候使用
    #keepalive_timeout  0;  #keepalive超时时间
    keepalive_timeout  65;
 
    #gzip  on; #开启gzip压缩服务
 
    #虚拟主机
    server {
        listen       80;  #配置监听端口号
        server_name  localhost; #配置访问域名,域名可以有多个,用空格隔开
        #charset koi8-r; #字符集设置
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #错误跳转页
        #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$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
        #    root           html; #根目录
        #    fastcgi_pass   127.0.0.1:9000; #请求转向定义的服务器列表
        #    fastcgi_index  index.php; # 如果请求的Fastcgi_index URI是以 / 结束的, 该指令设置的文件会被附加到URI的后面并保存在变量$fastcig_script_name中
        #    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_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m; 
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5; #密码加密方式
    #    ssl_prefer_server_ciphers  on; # ssl_prefer_server_ciphers  on; #
 
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

标注:此处转载于 https://blog.csdn.net/qq_37345604/article/details/90034424

 二、拿我一处配置好的项目给大家介绍一下,使用域名访问本地项目。

实际开发中,会有很多不同的环境,开发环境(本地环境)、测试环境(提供给测试人员用的环境)、预发布环境(数据是和生成环境的数据一致,运行最新的项目代码进去测试)、生产环境(项目最终发布上线的环境);

如果不同环境使用不同的ip去访问,可能会出现一些问题。为了保证所有环境的一致,我们会在各种环境下都使用域名来访问。

最终实现目标:本地web项目,通过指定的域名访问;

三、域名解析

一个域名一定会被解析为一个或多个ip。这一般会包含两步:

本地域名解析: 浏览器会首先在本机的hosts文件中查找域名映射的IP地址,如果查找到就返回IP ,没找到则进行域名服务器解析,一般本地解析都会失败,因为默认这个文件是空的。

  - Windows下的hosts文件地址:C:/Windows/System32/drivers/etc/hosts
  - Linux下的hosts文件所在路径: /etc/hosts 

域名服务器解析:

本地解析失败,才会进行域名服务器解析,域名服务器就是网络中的一台计算机,里面记录了所有注册备案的域名和ip映射关系,一般只要域名是正确的,并且备案通过,一定能找到。

四、解决域名解析问题

我们不可能去购买一个域名,因此我们可以伪造本地的hosts文件,实现对域名的解析。修改本地的host为:

```
127.0.0.1 api.leyou.com
127.0.0.1 manage.leyou.com
```

这样就实现了域名的关系映射了。

五、通过域名访问: http://localhost:9001/#/login  

重点:页面报错了  !Invalid Host header 

原因:我们配置了项目访问的路径,虽然manage.leyou.com映射的ip也是127.0.0.1,但是webpack会验证host是否符合配置。

所以,此处专有一篇介绍如何解决此问题

---》https://blog.csdn.net/weixin_43970743/article/details/103458047

六、nginx解决端口问题

比如:http://localhost:9001/#/login(前) --->   http://manage.leyou.com/#/login(后)

域名问题解决了,但是现在要访问后台页面,还得自己加上端口:`http://manage.leyou.com:9001/#/login`。
这就不够优雅了。我们希望的是直接域名访问:`http://manage.leyou.com/#/login`。这种情况下端口默认是80,如何才能把请求转移到9001端口呢?
这里就要用到反向代理工具:Nginx

七、什么是Nginx?

1、nginx可以作为web服务器,但更多的时候,我们把它作为网关,因为它具备网关必备的功能:

- 反向代理
- 负载均衡
- 动态路由
- 请求过滤

2、nginx作为反向代理

什么是反向代理?

- 代理:通过客户机的配置,实现让一台服务器(代理服务器)代理客户机,客户的所有请求都交给代理服务器处理。
- 反向代理:用一台服务器,代理真实服务器,用户访问时,不再是访问真实服务器,而是代理服务器。

nginx可以当做反向代理服务器来使用:

- 我们需要提前在nginx中配置好反向代理的规则,不同的请求,交给不同的真实服务器处理
- 当请求到达nginx,nginx会根据已经定义的规则进行请求的转发,从而实现路由功能

八、安装Nginx 

此处跳过、篇一详细介绍

https://blog.csdn.net/weixin_43970743/article/details/103460763

九、反向代理配置

```nginx
#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
   
    keepalive_timeout  65;

    gzip  on;
	server {
        listen       80;
        server_name  manage.leyou.com;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
			proxy_pass http://127.0.0.1:9001;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }
	server {
        listen       80;
        server_name  api.leyou.com;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
			proxy_pass http://127.0.0.1:10010;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }
}
```


解释一下:

十、测试

启动nginx,然后用域名访问后台管理系统:

现在实现了域名访问网站了,中间的流程是怎样的呢?

1. 浏览器准备发起请求,访问http://mamage.leyou.com,但需要进行域名解析

2. 优先进行本地域名解析,因为我们修改了hosts,所以解析成功,得到地址:127.0.0.1

3. 请求被发往解析得到的ip,并且默认使用80端口:http://127.0.0.1:80

   本机的nginx一直监听80端口,因此捕获这个请求

4. nginx中配置了反向代理规则,将manage.leyou.com代理到127.0.0.1:9001,因此请求被转发

5. 后台系统的webpack server监听的端口是9001,得到请求并处理,完成后将响应返回到nginx

6. nginx将得到的结果返回到浏览器

最终展示流程:

发布了85 篇原创文章 · 获赞 197 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/weixin_43970743/article/details/103461446