{"id":32757,"date":"2023-02-10T13:50:53","date_gmt":"2023-02-10T13:50:53","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32757"},"modified":"2023-02-10T13:50:55","modified_gmt":"2023-02-10T13:50:55","slug":"configure-nginx-in-host-server-with-https-enabled-and-route-all-traffic-container","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/configure-nginx-in-host-server-with-https-enabled-and-route-all-traffic-container\/","title":{"rendered":"configure nginx in host server with https enabled and route all traffic container"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Detail steps to configure nginx in host server with https enabled and route all traffic from nginx to docker container<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Here are the detailed steps to configure Nginx as a reverse proxy on a host server with HTTPS enabled and route all traffic from Nginx to a Docker container:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install Nginx on the host server:<\/strong><\/li>\n<\/ol>\n\n\n\n<p>You can install Nginx on your host server using your system&#8217;s package manager. For example, on a Debian-based system, you can use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>sudo apt-get install nginx\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Create an Nginx configuration file:<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Create a new Nginx configuration file in the <code>\/etc\/nginx\/sites-available<\/code> directory. You can name the file whatever you like, for example, <code>my_site<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopy code<code>sudo nano \/etc\/nginx\/sites-available\/my_site\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Configure Nginx:<\/strong><\/li>\n<\/ol>\n\n\n\n<p>In the Nginx configuration file, add the following code to configure Nginx as a reverse proxy:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">perlCopy code<code>server {\n    listen 80;\n    listen [::]:80;\n    server_name example.com;\n\n    location \/ {\n        proxy_pass http:\/\/&lt;docker_container_ip&gt;:&lt;docker_container_port&gt;;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>In the above code, replace <code>example.com<\/code> with your domain name and <code>&lt;docker_container_ip&gt;<\/code> and <code>&lt;docker_container_port&gt;<\/code> with the IP address and port number of your Docker container, respectively.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>Enable the Nginx configuration:<\/strong><\/li>\n<\/ol>\n\n\n\n<p>To enable the Nginx configuration, create a symbolic link from the configuration file to the <code>\/etc\/nginx\/sites-enabled<\/code> directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopy code<code>sudo ln -s \/etc\/nginx\/sites-available\/my_site \/etc\/nginx\/sites-enabled\/\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li><strong>Verify Nginx configuration:<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Verify that the Nginx configuration is valid by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Copy code<code>sudo nginx -t\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li><strong>Restart Nginx:<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Restart Nginx to apply the changes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Copy code<code>sudo systemctl restart nginx\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"7\">\n<li>Obtain SSL certificate:<\/li>\n<\/ol>\n\n\n\n<p>To enable HTTPS, you need to obtain an SSL certificate for your domain. You can obtain a free SSL certificate from Let&#8217;s Encrypt using tools like Certbot.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"8\">\n<li>Configure HTTPS:<\/li>\n<\/ol>\n\n\n\n<p>Add the following code to your Nginx configuration file to configure HTTPS:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cssCopy code<code>server {\n    listen 443 ssl;\n    listen [::]:443 ssl;\n    server_name example.com;\n\n    ssl_certificate \/path\/to\/certificate.crt;\n    ssl_certificate_key \/path\/to\/certificate.key;\n\n    location \/ {\n        proxy_pass http:\/\/&lt;docker_container_ip&gt;:&lt;docker_container_port&gt;;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>Replace <code>example.com<\/code> with your domain name, <code>&lt;docker_container_ip&gt;<\/code> and <code>&lt;docker_container_port&gt;<\/code> with the IP address and port number of your Docker container, and <code>\/path\/to\/certificate.crt<\/code> and <code>\/path\/to\/certificate.key<\/code> with the<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Detail steps to configure nginx in host server with https enabled and route all traffic from nginx to docker container Here are the detailed steps to configure&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-32757","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32757"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32757\/revisions"}],"predecessor-version":[{"id":32758,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32757\/revisions\/32758"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}