利用nginx解决Nuget无法访问的问题

2021/11/9 7:15:41

本文主要是介绍利用nginx解决Nuget无法访问的问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

利用nginx解决Nuget无法访问的问题

  • 1. 安装nginx
  • 2. 配置nginx
  • 3. 配置nuget源

通过nginx的代理能力,实现以下两个功能:

  1. 将api.nuget.org指向国内镜像源nuget.cdn.azure.cn。
  2. 将代理响应的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{}中注释掉的部分已实现相关配置,可自行实验。



这篇关于利用nginx解决Nuget无法访问的问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程