PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How do I set up an nginx conf with a subdomain and reverse_proxy and NO use of sites-enabled?

 June 25, 2022     nginx, reverse-proxy, subdomain     No comments   

Issue

I've read that it's not necessary to use sites-enabled, and even seen it suggested not to use.

In any case, its merit is not part of the question (so please consider that discussion off topic).

What I'm trying to do is set up an absolutely barebones basic nginx.conf file which does some super basic use case stuff: various forms of redirection.

By my understanding, this conf should be enough:

http {
  # default server
  server {
    root /var/www/html/production-site;

    # reverse proxy for external blog, makes example.com/blog display the blog.  helps with SEO.
    location /blog/ {
      proxy_pass https://example.some-external-blog.com/;
    }
  }

  # dev server
  server {
    server_name dev.example.com;
    root /var/www/html/dev-site;
  }
}

Unfortunately, my example doesn't work. The proxy bit works, but subdomains don't seem to. I don't honestly believe that server_name does anything at this point.

So, how does one write a simple (no extras) nginx.conf file which will exemplify these super trivial functionalities (subdomains and reverse proxies)?


Solution

I tried your config at my sandbox VM. nginx refuses to start, and when I run nginx -t command (which is always a good idea after significant configuration change), it says:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] no "events" section in configuration
nginx: configuration file /etc/nginx/nginx.conf test failed

So I've added events {} line to the config. After that nginx successfuly starts and all is working as expected.

Another thing that I wouldn't skip is including mime.types file. So final minimal configuration would look as follows:

events {}

http {
  include mime.types;

  # default server
  server {
    root /var/www/html/production-site;

    # reverse proxy for external blog, makes example.com/blog display the blog.  helps with SEO.
    location /blog/ {
      proxy_pass https://example.some-external-blog.com/;
    }
  }

  # dev server
  server {
    server_name dev.example.com;
    root /var/www/html/dev-site;
  }
}


Answered By - Ivan Shatsky
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing