1、Web应用使用Nginx进行代理,内网访问则直接是访问的Nginx:80代理服务器,在外网则访问不了,所以就有一个问题需要映射网络
2、通过路由器配置将Nginx:80代理服务器80端口映射到Web主机IP的8080端口,Web主机IP的80端口给了另外一个WebApp,所以请求主页面的时候Nginx总是site:8080/Webapp地址中的8080给自动去掉了导致404错误
3、问了度娘也问了谷歌发现
port_in_redirect off;参数可以防止自动把8080端口给去掉于是就加上,但是新的问题又来了,样式什么的不出来,查看网页源码发现里面的链接地址还是site:80/Webapp/app.css
4、同时在请求方法时候发现还是会自动把8080端口给自动去掉了变成了
site:80/Webapp/action导致404错误,于是继续度娘谷歌无果
5、在查看Nginx的一些参数的时候发现反向代理里面有一个参数
proxy_set_header Host $host;于是去查看了相关源码解释并将参数改成这样
proxy_set_header Host $host:8080;再次请求主页,并执行相应方法,所有问题均解决
以下是配置文件配置:
#user nobody;
worker_processes 4;
#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;
tcp_nodelay on;
keepalive_timeout 65;
port_in_redirect off;
gzip on;
upstream localhost {
server App1IP:18080;
server <span style="font-family:Arial, Helvetica, sans-serif;">App2IP:18081</span>
server App3IP:18082;
ip_hash;
}
server {
listen 80;
listen 8080;
server_name localhost web主机IP web主机域名;
charset utf-8;
#access_log logs/host.access.log main;
location ~ ^/NginxStatus/ {
stub_status on; #Nginx 状态监控配置
access_log off;
}
location ~ \.jsp$ {
proxy_pass http://localhost;
}
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_redirect off;
proxy_set_header Host $host:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 15;
proxy_send_timeout 15;
proxy_read_timeout 15;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}
location ~ /\.ht {
deny all;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
版权声明:本文为hjjoe1213123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。