看完本文会得到什么
恭喜你,你遇到了服务端配置里面第一个需要动脑子的环节。
我们先看看为什么 nginx + nps 同时安装并且不对他们俩做任何配置修改的时候,为什么会出现端口冲突?
首先我们看 nginx 的配置文件,众所周知,nginx 默认就是会监听你服务器上的 80 端口
$ cat /etc/nginx/sites-available/default
你会看到如下内容
## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # https://www.nginx.com/resources/wiki/start/ # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ # https://wiki.debian.org/Nginx/DirectoryStructure # # In most cases, administrators will remove this file from sites-enabled/ and # leave it as reference inside of sites-available where it will continue to be # updated by the nginx packaging team. # # This file will automatically load configuration files provided by other # applications, such as Drupal or Wordpress. These applications will be made # available underneath a path with that package name, such as /drupal8. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # #server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } #}
这里最关键的配置是
listen 80 default_server; listen [::]:80 default_server;
而 nps 的服务端的默认配置文件如何?
$ cat /etc/nps/conf/nps.conf
它的内容如下:
appname = nps #Boot mode(dev|pro) runmode = dev #HTTP(S) proxy port, no startup if empty http_proxy_ip=0.0.0.0 http_proxy_port=80 https_proxy_port=443 https_just_proxy=true #default https certificate setting https_default_cert_file=conf/server.pem https_default_key_file=conf/server.key ##bridge bridge_type=tcp bridge_port=8024 bridge_ip=0.0.0.0 # Public password, which clients can use to connect to the server # After the connection, the server will be able to open relevant ports and parse related domain names according to its own configuration file. public_vkey=123 #Traffic data persistence interval(minute) #Ignorance means no persistence #flow_store_interval=1 # log level LevelEmergency->0 LevelAlert->1 LevelCritical->2 LevelError->3 LevelWarning->4 LevelNotice->5 LevelInformational->6 LevelDebug->7 log_level=7 #log_path=nps.log #Whether to restrict IP access, true or false or ignore #ip_limit=true #p2p #p2p_ip=127.0.0.1 #p2p_port=6000 #web web_host=a.o.com web_username=admin web_password=123 web_port = 8080 web_ip=0.0.0.0 web_base_url= web_open_ssl=false web_cert_file=conf/server.pem web_key_file=conf/server.key # if web under proxy use sub path. like http://host/nps need this. #web_base_url=/nps #Web API unauthenticated IP address(the len of auth_crypt_key must be 16) #Remove comments if needed #auth_key=test auth_crypt_key =1234567812345678 #allow_ports=9001-9009,10001,11000-12000 #Web management multi-user login allow_user_login=false allow_user_register=false allow_user_change_username=false #extension allow_flow_limit=false allow_rate_limit=false allow_tunnel_num_limit=false allow_local_proxy=false allow_connection_num_limit=false allow_multi_ip=false system_info_display=false #cache http_cache=false http_cache_length=100 #get origin ip http_add_origin_header=false #pprof debug options #pprof_ip=0.0.0.0 #pprof_port=9999 #client disconnect timeout disconnect_timeout=60
这里最关键的配置是
http_proxy_port=80 https_proxy_port=443
所以他们哥俩默认都在抢占 80 端口。
找到了冲突的配置,我们就要修改一小部分,首先我们确认我们的思路
服务器上 80/443 端口,如果你有计划未来在服务器上做其他用处,那么就不建议 nps 占用这个端口,因为 nps 虽然也能做本地转发,但是比较费劲,你本地既要做服务端又要做服务端,多此一举。
另外也是最重要的一点
nginx 更适合做代理
我们把 nginx 当作第一道的看门人,它可以精准的代理这个星球上绝大多数的请求,而且它的相关代理配置你可以很轻松找到别人写好的配置,直接抄作业就好,nps 我们还是让他专司其职,专注于内网穿透。
而且未来如果你服务端其他端口有转发需求,你完全可以交给 nps,所以我们确定思路
我们只需要修改 nps 服务端的配置文件
$ vi /etc/nps/conf/nps.conf
修改内容如下
http_proxy_port=80
-> http_proxy_port=8080
# 让 nps 监听 8080 上的 http 内网穿透转发请求
https_proxy_port=443
-> https_proxy_port=8443
# 让 nps 监听 8443 上的 https 的内网穿透转发请求
web_port = 8080
-> web_port = 8090
# 这是 nps 自身的管理后台的端口,它不能跟其他端口冲突,否则你打不开管理控制台
修改完毕之后,重启刷新配置
# sudo nps reload
打开 nps 管理控制台
http://1.2.3.4:8090
如果你能看到控制台就说明一切 ok 了。