nginx 小白求助

2016-04-15 14:43:25 +08:00
 Jakesoft

求助: 不完全配置如下

server{ 
	listen       80;
	server_name server;
	location / { 
		proxy_pass       http://my.proxy.server; 
		proxy_set_header   Host            $host; 
		proxy_set_header   X-Real-IP        $remote_addr; 
		proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 	
	} 
}
server {
   listen  8080;
	server_name 127.0.0.1; 
	index  index.php index.html index.htm;
	root   /www;
	#pathinfo 模式支持
	include vhosts/rewrite/default.conf;
	allow 127.0.0.1;
	deny all;
}
upstream my.proxy.server{
	server 127.0.0.1:8080;
}

问题如下:

一个静态文件,路径是 http://server/a/index,

为什么访问 http://server/a 时跳到

http://server:8080/a/

而我不想把8080端口暴露出来,求教哪里出问题了?

2420 次点击
所在节点    NGINX
6 条回复
Havee
2016-04-15 14:54:19 +08:00
proxy_pass http://my.proxy.server:8080;
lhbc
2016-04-15 15:21:28 +08:00
proxy_redirect http://localhost:8080/ http://$host;
kookerjp
2016-04-15 15:32:44 +08:00
http {
port_in_redirect off;
Jakesoft
2016-04-15 15:57:10 +08:00
@kookerjp 你这个方法可以,但是我的网站使用了 https,导致 https://server/a 301 到 http://server/a/,然后继续 301 到 https://server/a/,第二个 301 是我自己配置的:

rewrite ^(.*)$ https://$host$1 permanent;

关键是第一个 301 是怎么来的,难道反向代理一个静态文件会直接 301 跳转吗?
Havee
2016-04-15 16:45:50 +08:00
试试这样
server {
listen 80;
server_name domain;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8080;
}
}

然后给 8080 端口的也写个 server
server {
listen 8080;
server_name 127.0.0.1;

location / {
root /www;
index index.php index.html index.htm;
}
}

如果不行,则在 8080 的 server 中加 port_in_redirect off;
server {
listen 8080;
server_name 127.0.0.1;
port_in_redirect off ;

location / {
root /www;
index index.php index.html index.htm;
}
}
Havee
2016-04-15 16:50:19 +08:00
443 端口应该这样

80 端口跳到 443 ,用 443 端口的 server 去 proxy_pass http://127.0.0.1:8080;

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/271337

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX