V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Jakesoft
V2EX  ›  NGINX

nginx 小白求助

  •  
  •   Jakesoft ·
    Jaggle · 2016-04-15 14:43:25 +08:00 · 2410 次点击
    这是一个创建于 2934 天前的主题,其中的信息可能已经有所发展或是发生改变。

    求助: 不完全配置如下

    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端口暴露出来,求教哪里出问题了?

    6 条回复    2016-04-15 16:50:19 +08:00
    Havee
        1
    Havee  
       2016-04-15 14:54:19 +08:00
    proxy_pass http://my.proxy.server:8080;
    lhbc
        2
    lhbc  
       2016-04-15 15:21:28 +08:00
    proxy_redirect http://localhost:8080/ http://$host;
    kookerjp
        3
    kookerjp  
       2016-04-15 15:32:44 +08:00
    http {
    port_in_redirect off;
    Jakesoft
        4
    Jakesoft  
    OP
       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
        5
    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
        6
    Havee  
       2016-04-15 16:50:19 +08:00
    443 端口应该这样

    80 端口跳到 443 ,用 443 端口的 server 去 proxy_pass http://127.0.0.1:8080;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1286 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 23:54 · PVG 07:54 · LAX 16:54 · JFK 19:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.