{"id":1876,"date":"2021-07-04T17:32:21","date_gmt":"2021-07-04T17:32:21","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=1876"},"modified":"2024-07-01T10:51:27","modified_gmt":"2024-07-01T10:51:27","slug":"nginx","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/linux\/nginx\/","title":{"rendered":"Nginx"},"content":{"rendered":"\n<h2>Location root vs alias<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>http {\n\n\tserver {\n\n\t\tlisten 8081;\n\t\troot \/var\/www\/html\/websites;\n\n\t\t#with root no need to specify in url as location automatically append to root\n\t\tlocation \/sqr {\n\t\t\troot \/var\/www\/html\/websites;\n\t\t}\n\n\t\t#with alias need to specify in url as location does not append to alias\n\t\tlocation \/test {\n\t\t\talias \/var\/www\/html\/websites\/vegetables\/;\n\t\t\ttry_files $uri veggies.html =404;\n\t\t}\n\n\t\t#with regex add try_files directive\n\t\tlocation ~* \/foo$ {\n\t\t\talias \/var\/www\/html\/websites\/fruits\/site;\n\t\t\ttry_files $uri $uri\/index.html =404;\n\t\t}\n\n\t\t#with regex add try_files directive above will serve only till foo but below code will serve after foo also e.g. http:\/\/site.com\/foo\/abc.html\n\t\tlocation ~* \/foo {\n\t\t\talias \/var\/www\/html\/websites\/fruits\/site;\n\t\t\ttry_files $uri $uri\/index.html =404;\n\t\t}\n\n\t}\n\n}\n\nevents {}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>nginx -s reload<\/code><\/pre>\n\n\n\n<p>Check using following url<\/p>\n\n\n\n<p>http:\/\/yoursite\/sqr<\/p>\n\n\n\n<p>http:\/\/yoursite\/test<\/p>\n\n\n\n<p>http:\/\/yoursite\/foo\/<\/p>\n\n\n\n<h2>Redirect and Rewrite<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#this will redirect to foo and also changes the URL\nlocation \/bar {\n\treturn 307 \/foo;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>#this will rewrite the url i.e. URL would not change URL will be fizz and content will load from foo \nrewrite \/fizz \/foo;<\/code><\/pre>\n\n\n\n<h2>Nginx as a Load Balancer<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Define an upstream block to group backend servers\nupstream backend_servers {\n    # List of backend servers\n    server backend1.example.com:8080;\n    server backend2.example.com:8080;\n    server backend3.example.com:8080;\n    server backend4.example.com:8080;\n}\n\n\n\nserver {\n    listen 80;  # Listen on port 80\n\n    # Define the server name (domain or IP)\n    server_name www.example.com;\n\n    # Location block to define how requests should be handled\n    location \/ {\n        # Use the defined upstream group for proxying requests\n        proxy_pass http:\/\/backend_servers;\n}<\/code><\/pre>\n\n\n\n<h2>Run from terminal<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>while true; do curl -s http:\/\/www.example.com; sleep 1; done;<\/code><\/pre>\n\n\n\n<h3>Example<\/h3>\n\n\n\n<p>you can run 2 servers locally using php -S or using docker containers by mapping 2 ports<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http {\n\n\tupstream backend_servers {\n\t\tserver localhost:1234;\n\t\tserver localhost:4567;\n\t}\n\n\n\tserver {\n\n\t\tlisten 8081;\n\t\t#root \/var\/www\/html\/websites;\n\n\n\t\tlocation \/ {\n\t\t\tproxy_pass http:\/\/backend_servers;\n\t\t}\n\n\n\t}\n\n}\n\nevents {}\n<\/code><\/pre>\n\n\n\n<h2>PHP FPM socket configuration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>user www-data;\n\nevents {}\n\nhttp {\n\n    root \/var\/www\/samosa;\n\n\n    server {\n\n        access_log \/var\/log\/nginx\/samosa-access.log;\n        error_log \/var\/log\/nginx\/samosa-error.log;\n\n        error_page 404 \/404.html;\n\n        location ~ \\.php$ {\n            include snippets\/fastcgi-php.conf;\n            fastcgi_pass unix:\/run\/php\/php8.1-fpm.sock;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>create project and verify with normal url<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/var\/www\/html\/myproject<\/code><\/pre>\n\n\n\n<p>vim index.html<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1&gt;Hello World&lt;\/h1&gt;<\/code><\/pre>\n\n\n\n<p>vim index.php<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nphpinfo();<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-1.png\"><img loading=\"lazy\" width=\"477\" height=\"335\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-1.png\" alt=\"\" class=\"wp-image-1878\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-1.png 477w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-1-300x211.png 300w\" sizes=\"(max-width: 477px) 100vw, 477px\" \/><\/a><\/figure>\n\n\n\n<p>Create server block conf file<\/p>\n\n\n\n<p>vim \/etc\/nginx\/conf.d\/myproject.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n  listen 8082 default_server;\n  server_name _;\n\n  index index.php index.html;\n\n  root \/var\/www\/html\/myproject;\n\n  location ~ \\.php$\n  {\n      include snippets\/fastcgi-php.conf;\n      fastcgi_pass unix:\/var\/run\/php\/php7.4-fpm.sock;\n  }\n}<\/code><\/pre>\n\n\n\n<p>Test nginx configuration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nginx -t<\/code><\/pre>\n\n\n\n<p>restart nginx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service nginx restart\nOR\nsudo systemctl restart nginx<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image.png\"><img loading=\"lazy\" width=\"1024\" height=\"404\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-1024x404.png\" alt=\"\" class=\"wp-image-1877\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-1024x404.png 1024w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-300x118.png 300w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-768x303.png 768w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image.png 1327w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>For Laravel Project <br>Specify path till public directory<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-2.png\"><img loading=\"lazy\" width=\"1024\" height=\"544\" src=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-2-1024x544.png\" alt=\"\" class=\"wp-image-1883\" srcset=\"https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-2-1024x544.png 1024w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-2-300x159.png 300w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-2-768x408.png 768w, https:\/\/codeinsightacademy.com\/blog\/http:\/\/codeinsightacademy.com\/blog\/wp-content\/uploads\/2021\/07\/image-2.png 1363w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>Need to explore<\/p>\n\n\n\n<p>worker process<\/p>\n\n\n\n<p>log format<\/p>\n\n\n\n<p>weight in upstream<\/p>\n\n\n\n<p>upstream block ip:port <strong>down<\/strong><\/p>\n\n\n\n<p>nginx plus<\/p>\n\n\n\n<p>directives &#8211; listen root index access_log error_log<\/p>\n\n\n\n<p>access_log <strong>off<\/strong>;<\/p>\n\n\n\n<p>error_log debug;<br>error_log warn;<\/p>\n\n\n\n<p>$scheme variable<\/p>\n\n\n\n<p>include \/etc\/nginx\/mime.types;<\/p>\n\n\n\n<p>nginx vs haproxy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Location root vs alias Check using following url http:\/\/yoursite\/sqr http:\/\/yoursite\/test http:\/\/yoursite\/foo\/ Redirect and Rewrite Nginx as a Load Balancer Run from terminal Example you can run 2 servers locally using php -S or using docker containers by mapping 2 ports PHP FPM socket configuration create project and verify with normal url vim index.html vim index.php [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1876"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=1876"}],"version-history":[{"count":18,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1876\/revisions"}],"predecessor-version":[{"id":2743,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/1876\/revisions\/2743"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=1876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=1876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=1876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}