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

Posted

技术标签:

【中文标题】Nginx 作为虚拟主机和反向代理【英文标题】:Nginx as virtual host and as reverse proxy 【发布时间】:2021-01-03 15:25:23 【问题描述】:

我在 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;
    

当我尝试通过在/etc/nginx/conf.d/ 中设置以下文件来为 PostfixAdmin 创建虚拟主机时,如here 所述。在 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;
   

【问题讨论】:

【参考方案1】:

我意识到我的服务器的设置方式,不可能实现我在上面尝试做的事情。无需为 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;
  


【讨论】:

以上是关于Nginx 作为虚拟主机和反向代理的主要内容,如果未能解决你的问题,请参考以下文章

nginx 反向代理 tomcat (https虚拟主机)

细说nginx系列之应用场景介绍

Nginx入门

Nginx正向代理反向代理负载均衡及性能优化

Nginx学习日记第四篇 -- 反向代理及缓存功能

nginx的虚拟主机和反向代理和一些技巧应用