利用nginx解决Nuget无法访问的问题
通过nginx的代理能力,实现以下两个功能:
- 将api.nuget.org指向国内镜像源nuget.cdn.azure.cn。
- 将代理响应的nuget.cdn.azure.cn/v3/index.json中的文本“api.nuget.org”自动替换为:“nuget.cdn.azure.cn”。
操作环境:deepin 20.4
1. 安装nginx
$sudo apt install nginx-full
2. 配置nginx
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip off;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
server {
listen 80;
server_name localhost;
#charset utf-8;
#location /forward {
# rewrite /forward/(.*)$ /$1 break;
# proxy_pass https://nuget.cdn.azure.cn/;
#}
location / {
#proxy_pass http://127.0.0.1/forward;
proxy_pass https://nuget.cdn.azure.cn/;
#proxy_set_header Accept-Encoding "";
sub_filter_types *;
sub_filter_once off;
sub_filter 'api.nuget.org' 'nuget.cdn.azure.cn';
}
}
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
}
3. 配置nuget源
#查看源列表
$ dotnet nuget list source
#添加源
$ dotnet nuget add source http://127.0.0.1/v3/index.json -n nuget.nginx
#禁用原有默认源
$ dotnet nuget disable source nuget.org
如果国内源今后采用了gzip压缩响应,可以参考:【解决 sub_filter 不能替换 Gzip 过的内容】的方法解决,上面的配置文件server{}
中注释掉的部分已实现相关配置,可自行实验。