如何更改 odoo 的 nginx 配置上下文 URI

How to change nginx configuration context URI for odoo

我的 odoo15 可以正常工作 URL -> https://vidatest.xyz 以下是 odoo 文档建议的 nginx 配置。

#odoo server
upstream odoo {
  server 127.0.0.1:8069;
}
upstream odoochat {
  server 127.0.0.1:8072;
}

# http -> https
server {
  listen 80;
  server_name vidatest.xyz;
  rewrite ^(.*) https://$host permanent;
}

server {
  listen 443 ssl;
  server_name vidatest.xyz;
  proxy_read_timeout 720s;
  proxy_connect_timeout 720s;
  proxy_send_timeout 720s;

  # Add Headers for odoo proxy mode
  proxy_set_header X-Forwarded-Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header X-Real-IP $remote_addr;

  # SSL parameters
  ssl_certificate /etc/certs/certs_14March2022/98fa7f5df02b430e.crt;
  ssl_certificate_key /etc/certs/generated-private-key.key;
  ssl_session_timeout 30m;
  ssl_protocols TLSv1.2;
  ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
  ssl_prefer_server_ciphers off;


  # log
  access_log /var/log/nginx/odoo.access.log;
  error_log /var/log/nginx/odoo.error.log;

  # Redirect longpoll requests to odoo longpolling port
  location /longpolling {
    proxy_pass http://odoochat;
  }

  # Redirect requests to odoo backend server
  location / {
    proxy_redirect off;
    proxy_pass http://odoo;
  }

  # common gzip
  gzip_types text/css text/scss text/plain text/xml application/xml application/json application/javascript;
  gzip on;
}

但是,我的要求是 https://vidatest.xyz 不是用于 odoo,而是用于我的应用程序。

odoo 应该可以解决这个问题 URL -> https://vidatest.xyz/web

因此,我更新并添加了以下更改到 nginx 配置

  # Redirect requests to odoo backend server
  location /web {
    proxy_redirect off;
    proxy_pass http://odoo;
  }

  # Redirect requests to odoo backend server
  location / {
    proxy_redirect off;
    proxy_pass http://mybank.com;
  }

现在,odoo 正在为此 URL -> https://vidatest.xyz/web 加载,但在成功登录后抛出以下错误

错误堆栈:

POST https://vidatest.xyz/mail/init_messaging 405 (Not Allowed)
POST https://vidatest.xyz/longpolling/poll 405 (Not Allowed)
Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.<anonymous> (web.assets_backend.min.js:585:55)
(anonymous) @ web.assets_backend.min.js:585
load (async)
(anonymous) @ web.assets_backend.min.js:583
jsonrpc @ web.assets_backend.min.js:582
rpc @ web.assets_backend.min.js:590
(anonymous) @ web.assets_backend.min.js:3324
legacyEnv.session.rpc @ web.assets_backend.min.js:3323
(anonymous) @ web.assets_common.min.js:5057
rpc @ web.assets_common.min.js:5057
_trigger_up @ web.assets_backend.min.js:5924
trigger_up @ web.assets_common.min.js:4855
call @ web.assets_common.min.js:4908
_rpc @ web.assets_common.min.js:4908
_makePoll @ web.assets_backend.min.js:5914
_poll @ web.assets_backend.min.js:5914
startPolling @ web.assets_backend.min.js:5913
startPolling @ web.assets_backend.min.js:5893
prototype.<computed> @ web.assets_common.min.js:4716
_startElection @ web.assets_backend.min.js:5900
startPolling @ web.assets_backend.min.js:5890

Odoo 和 python 版本:

root@ip-172-31-42-237:/var/log# systemctl status odoo
● odoo.service - Odoo15
     Loaded: loaded (/etc/systemd/system/odoo.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-04-06 10:26:20 UTC; 6min ago
   Main PID: 420828 (python3)
      Tasks: 4 (limit: 4623)
     Memory: 138.1M
     CGroup: /system.slice/odoo.service
             └─420828 /opt/odoo/odoo-venv/bin/python3 /opt/odoo/odoo/odoo-bin -c /etc/odoo.conf -u vida_product

python3 --version
Python 3.8.10

/usr/bin/pip --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

您尝试做的事情有点棘手。您可能无法实现它,因为 Odoo 使用 /web 作为转发到后端的主要分母。您正在尝试服务:

  • https://vidatest.xyz - 另一个网站
  • https://vidatest.xyz/web - odoo

但 Odoo 将完整域或子域视为自己的域并照此进行管理。这就是您低于错误的原因。

POST https://vidatest.xyz/mail/init_messaging 405 (Not Allowed)
POST https://vidatest.xyz/longpolling/poll 405 (Not Allowed)

您的问题更多是 Nginx 问题而不是 Odoo 问题。最简单的解决方案是将 Odoo 放在子域中,并从主域中提供您想要的任何内容。例如:https://odoo.vidatest.xyz Odoo 发出的每次调用都会指向该子域。

如果您坚持要部署的方式,请在启用开发人员模式后检查Settings > Technical > System Parameters中的web.base.url关键参数。将值设置为您要使用的完整域,如果不存在则创建它。并添加另一个参数,键 web.base.url.freeze 和值 True。看看这是否适合你。如果不是,您可能需要将引用的调用从 Nginx 重定向到您正在使用的完整路径。此外,您可能还需要设置长轮询端口的路径才能与 /web/longpolling 一起使用。我不确定,但最终的工作版本可能是 https://vidatest.xyz/web/web,所以它应该很棘手。