背景:项目需要一个服务器上部署多个应用。导致不同的应用需要使用不同的端口。如一些使用80,一些使用81或8080端口。如果域名指向后,仍然需要带端口访问。某个平台对带端口的地址不友好,如微信公众号无法跟带端口域名进行后台绑定。
需求:现有两个项目博客和监控分别80、8080的端口。暂时假设用www.focrs.com指向IP
博客的地址:www.fcors.com
监控地址:www.fcors.com:8080
如何把www.fcors.com:8080换成monitor.fcors.com?
则需要Nginx反向代理。
1、下载Nginx
Nginx官网 http://nginx.org/en/download.html ,本教程以:
nginx/Windows-1.18.0为例子
下载后解压缩
解压缩后打开D:\nginx\conf\nginx.conf
#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 80;
###########修改此处 server_name###########
server_name monitor.fcors.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
###########修改此处 proxy_pass(代理转发)###########
proxy_pass http://127.0.0.1:8999;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.htmls
#
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;
# }
#}
}
2、域名DNS解析(此处教程忽略)
3、启用Nginx
PS扩展。最好把Nginx做成windows服务,设置自动启动
windows利用winsw将Nginx配置到服务中
本教程以 2.11为演示https://github.com/winsw/winsw/releases/tag/v2.11.0
下载后,把文件更新为:nginx_service.exe。
创建文件nginx-service.exe.config和 nginx_service.xml
<configuration>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0" />
</startup>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
</configuration>
nginx-service.exe.config
<service>
<id>nginx</id>
<name>Nginx Service</name>
<description>High Performance Nginx Service</description>
<logpath>D:\nginx\servicelogs</logpath>
<log mode="roll-by-size">
<sizeThreshold>10240</sizeThreshold>
<keepFiles>8</keepFiles>
</log>
<executable>D:\nginx\nginx.exe</executable>
<startarguments>-p D:\nginx</startarguments>
<stopexecutable>D:\nginx\nginx.exe</stopexecutable>
<stoparguments>-p D:\nginx\nginx.exe -s stop</stoparguments>
</service>
安装服务