ubuntu - Nginx 作为虚拟主机和反向代理

标签 ubuntu nginx reverse-proxy virtualhost

我在 Ubuntu 服务器上运行 GlassFish(一个 Java 应用程序服务器)。我还在不同的端口上运行 phppgadmin,比如 5555 并配置了 apache。

我在 GlassFish 前面使用 Nginx 作为反向代理。 (尚未设置端口 443 的 SSL 和配置。)

server {
  listen          80;
  server_name     my_domain;

  location / {
        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_pass http://my_ip_number:8080;
  }
  
  location /phppgadmin {
        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_pass http://my_ip_number:5555/phppgadmin;
  }  
}

当我尝试为 PostfixAdmin 创建虚拟主机时,如描述的 here通过在 /etc/nginx/conf.d/ 中设置以下文件。在 http://postfixadmin.my_domain/setup.php 中,我只看到来自 glassfish 的 404 页面。
子域的 DNS A 记录已设置。
我的配置有什么问题吗?如何解决这个问题?

提前致谢

server {
   listen 80;
   listen [::]:80;
   server_name postfixadmin.my_domain;

   root /usr/share/postfixadmin/public/;
   index index.php index.html;

   access_log /var/log/nginx/postfixadmin_access.log;
   error_log /var/log/nginx/postfixadmin_error.log;

   location / {
       try_files $uri $uri/ /index.php;
   }

   location ~ ^/(.+\.php)$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
   }
}

最佳答案

我意识到我的服务器的设置方式,不可能实现我上面尝试做的事情。不需要为 PostfixAdmin 创建虚拟主机!

这是我的最终配置,一切正常。

server {
  listen          my_ip_number:80;
  server_name     example.com www.example.com;

  include snippets/letsencrypt.conf;
  return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    return 301 https://example.com$request_uri;
}


server {
    listen 443 ssl http2;

    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    access_log  /var/log/nginx/example.com.access.log; # upstreamlog;
    error_log   /var/log/nginx/example.com.error.log;
    rewrite_log on;

  location / {
        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 HTTPS "on";

        proxy_pass http://my_ip_number:8080;
  }

  location /phppgadmin {
        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_pass http://my_ip_number:5555/phppgadmin;
  }


  location /mypostfixadmin {
        proxy_pass http://my_ip_number:5555/postfixadmin;

        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        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-Scheme $scheme;

        client_max_body_size 0;
  }

}

关于ubuntu - Nginx 作为虚拟主机和反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63926101/

相关文章:

ubuntu - 在 VS Code 中,终端不显示 Ubuntu 20.04 shell 的选项

github 在 ubuntu 中寻找应用程序?

php - 在没有http的情况下从同一服务器上的php向ngnix发送信息

nginx - 带有 nginx 和 Play 2.1 应用程序的子目录的反向代理

http - Nginx proxy_cache_key $request_body 被大请求体忽略

mysql - 问题 无法登录MySQL服务器

java - 在运行 statWeblogic.sh 我遇到了 dcevm 的问题

iis - 是否有用于 IIS 平滑流的 HTTP 反向代理可以终止 SSL,但使用 HTTP 检索媒体

python - 为什么 nginx 没有启动我的服务?

redirect - Nginx 在位置下返回