在nginx配置文件中只使用端口80进行https POST是否有方法?

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

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

问题

So, I have a 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.

英文:

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;
    }
}
英文:

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-2.html
匿名

发表评论

匿名网友

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

确定