本文共 3537 字,大约阅读时间需要 11 分钟。
安装并配置Nginx服务器
安装Nginx服务器之前,请确保系统已安装必要的软件包。如果已有Nginx服务运行,建议先停止服务以避免配置冲突。
停止Nginx服务命令:
./usr/local/nginx/sbin/nginx -s stop
修改Nginx配置文件:
vi /usr/local/nginx/conf/nginx.conf
配置完成后的Nginx配置文件内容如下:
# 用户信息设置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 { client_max_body_size 100M; 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和tcp_nopush(可选) sendfile on; tcp_nopush on; # 设置超时时间(可选) keepalive_timeout 65; # 启用Gzip压缩(可选) gzip on; # 配置后台接口反向代理 upstream tomcat8080_api { server 127.0.0.1:8080 weight=1; } server { listen 80; server_name 后台接口域名; location / { proxy_pass http://tomcat8080_api; proxy_redirect default; # 设置主机头和客户端真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 管理员后台反向代理配置 upstream tomcat8081_admin { server 127.0.0.1:8081 weight=1; } server { listen 80; server_name 管理员后台域名; location / { proxy_pass http://tomcat8081_admin; proxy_redirect default; } # 解决跨域问题 location /apis { rewrite ^/apis/(.*) $1 break; include uwsgi_params; proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 处理管理员后台域名2 server { listen 80; server_name 管理员后台域名2; location / { proxy_pass http://tomcat8081_admin; proxy_redirect default; } location /apis { rewrite ^/apis/(.*) $1 break; include uwsgi_params; proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 处理管理员后台域名3 server { listen 80; server_name 管理员后台域名3; location / { proxy_pass http://tomcat8081_admin; proxy_redirect default; } location /apis { rewrite ^/apis/(.*) $1 break; include uwsgi_params; proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # 商家后台反向代理配置 upstream tomcat8082_shanghu { server 127.0.0.1:8082 weight=1; } server { listen 80; server_name 商家后台域名; location / { proxy_pass http://tomcat8082_shanghu; proxy_redirect default; } # 解决跨域问题 location /apis { rewrite ^/apis/(.*) $1 break; include uwsgi_params; proxy_pass http://127.0.0.1:8080; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }} 启动Nginx服务:
./usr/local/nginx/sbin/nginx
重启Nginx服务:
./usr/local/nginx/sbin/nginx -s reload
以上操作完成后,请确保相关域名解析配置正确,且后端服务(如Tomcat)已运行。如有问题,请根据错误日志或访问接口进行排查。
转载地址:http://qkjfk.baihongyu.com/