Search code examples
nginxurlruntime-erroruriodoo-15

How to change nginx configuration context URI for odoo


My odoo15 works fine for this URL -> https://vidatest.xyz and below is the nginx configuration as suggested by odoo documentation.

#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$1 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;
}

However, my requirement is to use https://vidatest.xyz not for odoo but for my application.

odoo should work on this URL -> https://vidatest.xyz/web

Thus, I updated & added the below changes to the nginx configuration

  # 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;
  }

Now, odoo is loading for this URL -> https://vidatest.xyz/web but throws the below error after successful login

enter image description here

Error stack:

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 versions:

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)

Solution

  • What you are trying to do is a bit tricky. You may not be able to achieve it because Odoo uses /web as the main denominator to forward to backend. You are trying to serve:

    • https://vidatest.xyz - another website
    • https://vidatest.xyz/web - odoo

    But Odoo uses the full domain or the subdomain as its own and manages it as such. That is why you are getting below error.

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

    Your problem is more of a Nginx problem rather than an Odoo problem. The easiest solution is to put Odoo in a subdomain and serve whatever you want from the main domain. For example: https://odoo.vidatest.xyz Than each call Odoo makes would be to that subdomain.

    If you insist the way you want to deploy, check web.base.url key parameter in Settings > Technical > System Parameters after enabling developer mode. Set the value to the full domain you want to use, if it doesn't exists create it. And also add another parameter with key web.base.url.freeze and value True. See if that works for you. If it doesn't you may need to redirect the referred calls from Nginx to the full path you are using. Also, you may need to set the path of the longpolling port to work with /web/longpolling as well. I am not sure but final working version might be https://vidatest.xyz/web/web so it should be tricky.