nginx 反向代理同一个域名的不同端口

2018-10-10 00:57:14 +08:00
 Hzzone

我的需求是:

  1. 同一个域名范文,通过指定 URL 访问具体的端口
  2. 不使用二级域名
  3. 全局 https,强制 http 转 https

现在我解决了,http 和 https 共存,且可以通过 http 访问指定 URL,但是当 https 访问时会 404

我的配置文件如下:

# sudo certbot certonly --webroot -w /usr/share/nginx/html/ -d hzzone.io
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;
	gzip_disable "msie6";


	
	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;

	server {
		listen 80;
        listen       443 ssl;

        # 域名,实际情况下时,将这个改成域名
        server_name  hzzone.io;

        ssl on;

        # 证书位置
        ssl_certificate  /etc/letsencrypt/live/hzzone.io/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/hzzone.io/privkey.pem;

        location /api {
	        proxy_pass http://localhost:1111/api;
	        proxy_http_version 1.1;
	        proxy_set_header Upgrade $http_upgrade;
	        proxy_set_header Connection "Upgrade";
	        proxy_set_header X-Real-IP $remote_addr;
	    }

	    location / {
	        proxy_pass https://localhost;
	    }
    }
}

为什么当我使用 http 访问 域名 /api/子目录 时,是正常的,而 https 却 404 呢?我看了 access log,根本没有转发。

请问是什么原因导致的这个问题?

7502 次点击
所在节点    NGINX
12 条回复
Hzzone
2018-10-10 01:05:45 +08:00
我的 python 代码如下:
```python
from flask import Flask
app = Flask(__name__)

@app.route('/api/latex')
def hello_world():
return 'Hello World!'

if __name__ == "__main__":
context = ('/etc/letsencrypt/live/hzzone.io/fullchain.pem', '/etc/letsencrypt/live/hzzone.io/privkey.pem')
# app.run(host='0.0.0.0', ssl_context=context, port=1111)
app.run(host='0.0.0.0', ssl_context=context, port=1111)
```

即使我 flask 也使用 https,http 访问 502 Bad Gateway,https 访问 404 Not Found,即使我改成 `http://localhost:1111/api;`
wly19960911
2018-10-10 01:16:22 +08:00
location / 转发到 443 端口,这个有什么意义?自己转发到自己不就死循环了,你看了 error.log 吗
wly19960911
2018-10-10 01:18:36 +08:00
如果你想实现 http 转 https 可以用 rewrite 进行 301 跳转。

一个 server 上配两个端口行不行得通我不知道,一般 HTTP 和 https 的 server 是分开配置的
msg7086
2018-10-10 07:17:13 +08:00
proxy_pass https://localhost;
这是要干嘛……
yidinghe
2018-10-10 08:31:09 +08:00
location / 这个地方有问题。如果是针对 http 的,你需要把两个端口分开来配。
Hzzone
2018-10-10 09:21:06 +08:00
@wly19960911
@msg7086
```
2018/10/10 09:18:08 [error] 1798#1798: *4 open() "/usr/share/nginx/html/api/latex" failed (2: No such file or directory), client: 182.148.57.110, server: hzzone.io, request: "GET /api/latex HTTP/1.1", host: "hzzone.io"
```

把这几行删了之后还是一样的呀,404
我想做的事对指定 URL 转发到本机上的其他端口
Hzzone
2018-10-10 09:21:45 +08:00
@yidinghe 分两个 server 吗??
zarte
2018-10-10 09:34:50 +08:00
https 不能多个端口吧
maojy1989
2018-10-10 09:35:25 +08:00
server {
listen 80;
server_name xxxx.com;
server_name www.xxxx.com;
access_log /home/wwwlogs/xxxx.access.log;
root /home/wwwroot/xxxx;
include enable-php.conf;
location / {
return 301 https://www.xxxx.com$request_uri;
}
}
server {
listen 443 ssl http2;
server_name www.xxxx.com;
root /home/wwwroot/xxxx;
ssl_certificate xxxx.pem;
ssl_certificate_key xxxx.key;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.2;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
include enable-php.conf;
location / {
include wordpress.conf;
index index.php index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:3000/api;
}
}
Hzzone
2018-10-10 09:38:35 +08:00
此贴结束,最终配成功了,需求如下:
http 全部转 https
指定 url 转发,例如 域名 /api/latex 转发 域名:1111/api/latex

配置文件如下:
```
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}
http {
server {
listen 80;
server_name 域名;
rewrite ^/(.*) https://hzzone.io/$1 permanent;
}

server {
listen 443;

ssl on;
ssl_certificate /etc/letsencrypt/live/hzzone.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/hzzone.io/privkey.pem;

server_name 域名;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

# location / {
# checks for static files; if not found, proxy to app
# try_files $uri @proxy_to_app;
# }

location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

proxy_pass http://localhost:1111/api;
}

}
}
```
Hzzone
2018-10-10 09:39:09 +08:00
@maojy1989 谢谢哈,我已经配好了
Les1ie
2018-10-10 14:54:01 +08:00
ssh 账号给我 我去帮你配

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

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

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

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

© 2021 V2EX