大网数据广告活动
服务器运维问答

Nginx listen to multiple ports but rewrite to one

· 52CE 我爱测

So I have an application running that for legacy reasons must keep it's old url with a trailing port number working. So I have a configuration similar to this: server { listen 443 ssl; listen 8080 ssl; server_name myurl.com; ssl …

问题原文

原帖链接 · inimene · CC BY-SA 3.0

So I have an application running that for legacy reasons must keep it's old url with a trailing port number working.

So I have a configuration similar to this:

server {
        listen 443 ssl;
        listen 8080 ssl;
        server_name myurl.com;

        ssl                     on;
        ssl_certificate         /etc/nginx/conf.d/certfile.pem;
        ssl_certificate_key     /etc/nginx/conf.d/keyfile.key;
        ssl_session_timeout     5m;
        ssl_session_cache       shared:SSL:10m;
        ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers             HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

        location / {

        proxy_pass              http://127.0.0.1:7990;
        proxy_http_version      1.1;
        proxy_set_header        Host $host;
        proxy_set_header        X-Forwarded-Host $host;
        proxy_set_header        X-Forwarded-Server $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        Location $host:443;
        proxy_redirect          off;
        }

}

It is working fine exept for the fact that some AJAX request made from myurl.com:8080 will throw CRSF errors. I would like to make it so that when a user connect with 8080 port, nginx will rewrite it to 443 automatically. Is this possible to do? I have tried adding various headers but none of them seemed to do the trick.

Also tried to do a return 301 https://$server_name:443$request_uri; but nginx did not like it really.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

You could break the server block into two, one for each port. Move common configuration into the outer block (if these are the only server blocks you have) or include common configuration with an include directive.

For example:

ssl_certificate         /etc/nginx/conf.d/certfile.pem;
ssl_certificate_key     /etc/nginx/conf.d/keyfile.key;
ssl_session_timeout     5m;
ssl_session_cache       shared:SSL:10m;
ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers             HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers   on;

server {
    listen 8080 ssl;
    server_name myurl.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name myurl.com;

    location / { ... }
}

The return directive does not need a port specification if the https port is 443. If nginx insists on adding port 8080, try adding a port_in_redirect off; statement.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。