在nginx配置文件中,是否有一种方法可以使用80端口进行HTTPS的POST请求?

huangapple go评论84阅读模式
英文:

Is there a way to make a https post with only port 80 in nginx config file?

问题

所以,我有一个Django应用程序,需要将数据发送到一个API。
我正在使用request来发送数据。
起初,它警告我-简而言之-我没有使用SSL来发送JSON数据。

为了解决这个问题,我添加了"verify=证书文件路径"。

requests.post(url, json=payload, verify='\fullChainCert.crt')

之后,我得到了200的响应,所以我很高兴。

问题是,这样在服务器上能行吗?

服务器上的443端口是开放的,但在nginx配置文件中只有80端口。

英文:

So, I a have Django app which needs to send data to an API.
I am using request to send the data.
First, it was warning me that -in short- I am not using SSL for sending the JSON data.

To solve this I added "verify=PATH TO THE CERT FILE"

requests.post(url, json=payload, verify='\fullChainCert.crt')

After this I got 200, so I was happy.

Question is, will it work like this on server?

Port 443 is open on server, but only 80 in nginx config file.

答案1

得分: 0

你可以配置nginx.conf文件以使用443端口进行https工作,并将流量从80端口重定向到443端口。一个简单的https配置如下所示:

server {
    listen 80;
    server_name yourservername.com或your_public_ip;
    return 301 https://$host$request_uri;   
}

server {
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    server_name yourservername.com或your_public_ip;

    # 在这里创建SSL证书并使用它们
    ssl_certificate        /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key    /etc/ssl/private/nginx-selfsigned.key;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    location / {
        proxy_pass http://django_app;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

请注意,你需要根据实际情况替换yourservername.comyour_public_ip为你的服务器名称或公共IP地址,并根据需要创建和配置SSL证书。

英文:

You can configure the nginx.conf file to work with https 443 port and redirect traffic coming to port 80 to port 443. A simple https configuration looks like this:

server {
    listen 80;
    server_name yourservername.com or your_public_ip;
    return 301 https://$host$request_uri;   
}

server {
    listen 443 http2 ssl;
    listen [::]:443 http2 ssl;

    server_name yourservername.com or your_public_ip;
 
    # You should create the ssl certificates and use them here
    ssl_certificate        /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key    /etc/ssl/private/nginx-selfsigned.key;
    ssl_dhparam /etc/ssl   /certs/dhparam.pem;

	location / {
        proxy_pass http://django_app;
	    proxy_set_header Host $host;
	    proxy_set_header X-Real-IP $remote_addr;
   	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_set_header X-Forwarded-Proto $scheme;
    }
}

huangapple
  • 本文由 发表于 2023年8月9日 13:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定