一:上面已经安装好了nginx可以,也可以访问:http://localhost:81地址
二:启动一个本地服务,例如:core  api服务。地址:http://localhost:62557/ 

三:配置nginx

 

第一种配置,不搭建建群。在nginx就代理一个http://localhost:62557/ 地址,

我们来看看最原始的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       81;
         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$ {
         #    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_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;
     #    }
     #}}


 

是这样的。如果只是单独的配置对应某一个站点:那么只需要修改一句话就ok

nginx 对get 上的路由长度有限制码 nginx 路由配置_html

找到 http--》server 节点。里面有一个 location/ 这样的节点。修改一下里面的内容。

原始配置是:

location / {
             root   html;
             index  index.html index.htm;
         }

修改为这样就可以:

location / { 
            proxy_pass http://localhost:62557/;
        }

这个时候,你启动了62557服务之后,可以通过nginx去访问。

例如:http://localhost:62557/api/WeatherForecast可以用

http://localhost:81/api/WeatherForecast 这样去访问,就做到了代理的作用。

如图:是不是2个不同的网址,访问的路径是一样的。

nginx 对get 上的路由长度有限制码 nginx 路由配置_html_02

 

nginx 对get 上的路由长度有限制码 nginx 路由配置_php_03

这样的配置,其实并不是大家想要的结果,我们要的是搭建集群之后,可以实现 轮询 或者权重 或者 Iphash等等不同的访问服务的算法。

这个时候就需要使用启动3台不同的服务器了。

可以用命令多启动几个服务,在相对的目录下cmd

命令:dotnet AspNetCore.WebApi.dll --urls="http://localhost:62557";

          dotnet AspNetCore.WebApi.dll --urls="http://localhost:62558";

           dotnet AspNetCore.WebApi.dll --urls="http://localhost:62559";

启动3个服务。保存服务正常,这里最好建议使用 cmd命令启动,如果自动在vs2019里面启动,有时候nginx不能发现服务,报网关错误,不知道为什么。这是一个坑。不要踩就ok

配置:如下

主要是 1 :需要在http节点下添加一个 upstream节点,里面的内容是:

 #轮询
                upstream backend{
                    server    localhost:62557; 
                    server    localhost:62558;
                    server    localhost:62559;  
                }

可以看下,里面会有如下2中方法,在配置中,只是注释了

 #权重
                #upstream backend{
                   # server    localhost:62557  weight=10; 
                   # server    localhost:62558  weight=1;
                    #server    localhost:62559  weight=1;  
                #}
                
   #iphash 此策略可保证同一访问ip落到到一机器上,从而解决分布式session问题
                #upstream backend{
                 # ip_hash;    #保证每个访客固定访问一个后端服务器
                #    server localhost:62557;
                #    server localhost:62558;
                #    server localhost:62559;
                #}

然后修改 server节点下面的location /  节点。记住 这个节点下面的   proxy_pass后面的  backend 必须跟上面upstream 命名的backend 保持一致,就ok

location / { 
           proxy_pass http://backend/;
       } 

 

#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;
     
      #轮询
                 upstream backend{
                     server    localhost:62557; 
                     server    localhost:62558;
                     server    localhost:62559;  
                 }
                 #权重
                 #upstream backend{
                    # server    localhost:62557  weight=10; 
                    # server    localhost:62558  weight=1;
                     #server    localhost:62559  weight=1;  
                 #}
                 
                 #iphash 此策略可保证同一访问ip落到到一机器上,从而解决分布式session问题
                 #upstream backend{
                  # ip_hash;    #保证每个访客固定访问一个后端服务器
                 #    server localhost:62557;
                 #    server localhost:62558;
                 #    server localhost:62559;
                 #}    server {
         listen       81;
         server_name  localhost;        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        
         
         location / { 
            proxy_pass http://backend/;
        }         #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_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;
     #    }
     #}}

这里需要注意点:

如果配置了:proxy_pass   那么再打开 http://localhost:81地址,就找不到页面了,但是服务其实是启动的。这里为什么呢?

proxy_pass 这玩意代表所有请求去应用服务器,index代表默认访问路径,你直接搞个index.html不存在的页面肯定访问不到,

不要proxy_pass,则代表不转发,默认是index.html,nginx也内置了一个这个index.html页面