WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、实现转发

打开conf下的nginx.conf文件,如下图:

 

2、添加.net core网站的转发

按下面的进行修改,修改完后,就把localhost的80转发到了https://localhost:5004的.net core应用上了。

复制代码
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass https://localhost:5004;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
复制代码

 

3、同一个端口下添加多个.net core应用

设置完后,就可以通过 localhost:83 访问 https://localhost:5004的网站,通过localhost:83/api 访问https://localhost:44378的网站

复制代码
    server {
        listen       83;
        server_name  localhost;
        
        location / {
            proxy_pass https://localhost:5004;
        }
        
        location ^~/api/ {
            proxy_pass https://localhost:44378/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
复制代码

 注意二级目录转的时候,子目录后面要有斜扛,转发的那个地址后面也要有斜扛,如下:

     location /data/ {
            proxy_pass https://localhost:5006/;
        }

 

4、同一个端口,多个域名的支持

复制代码
    server {
        listen       80;
        server_name  www.aaa.com;
        
        location / {
        proxy_pass http://localhost:8081;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       80;
        server_name  www.bbb.com;
        
        location / {
        proxy_pass http://localhost:8082;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
复制代码

 5、实现https支持

复制代码
    server {
        listen       443 ssl;
        server_name  localhost;

    ssl_certificate ..\ssl\5859212_www.aaa.com.pem;

    ssl_certificate_key ..\ssl\5859212_www.aaa.com.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://localhost:8081;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
复制代码

 如果想实现http自动跳转到https,可以再加一个server,如下:

    server {
    listen 80;
    server_name  localhost;
    rewrite ^(.*) https://$server_name$1 permanent;
    }

  6、在IdentityServer4中的使用

 如果想在identityserver4里使用必须加上下面的逻辑

复制代码
     location / {
            proxy_pass https://localhost:5005;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
            proxy_buffering off;
            expires           0;
        }
复制代码