Basic settings of Nginx

閱讀時間約 7 分鐘
Recently, I add some useful settings for my nginx web server. I would like to take a note of some basic and useful settings of nginx.

http server

First of all, let’s set up the most basic set. Please open your nginx.conf , the basic settings of http server part would be as follows.
Set the port number you want to deploy your web server at listen . Set the domain name you bought from Google Domains, GoDaddy, or just localhost at server_name.
The location / in the server block is to set where the root URL would go. The root is the path where you put your web bundle on your server.

https server

Let’s set up another basic setting. I think the most common and easiest way to get the ssl certificate is using Let’s Encript. Please see the details at https://medium.com/@xdxxxx4713/lets-encrypt-in-nginx-85866b848c8e.
The basic config of https should be as follows. Set the port number 443 and ssl at listen . Normally, the certificates would be located at some path like /etc/letsencrypt/live/your_domain_name. There are two .pem files here. Just set them as follows.

http redirect to https

To prevent user access by http, we need to modify the http server config a little bit. Remove the location setting we mention before, and set return 301 to your https server. Even user access your web by http, it would be redirected to https right now.

virtual host

In reality, it’s not quite possible only to deploy one web service on a server. Let’s talk about how to deploy multi web services on the same server.
Let’s make a directory called sites-available and another call sites-enabled under the path of nginx.
In sites-available , make a file just call your_domain_name . Then set the http and the https config as we mentioned before in this file.
If you want to put this web server online, make a link to nginx/sites-enabled .
In nginx.conf , set include sites-enabled/*; at the last of http block. Then nginx would apply all the settings from sites-enabled .

block direct access by ip

Once you put your website online, there would have many attacks every day. In my experience, most attacks are from bots. It just scans every IP and send the request. Even though most of them are harmless, they are still annoying.
We can block these requests by blocking direct access by IP. Users can only access your site by the domain name.
Just put the below settings to one of your web server config. Set default to listen to make this web server as your default server. Set server_name _; means direct access by IP. Finally, we return 403; . It would make the trick of blocking direct access by IP work.

solve refresh 404 issue

Another issue that should be solved is the refresh 404 problem. All the settings above are about the root URL. Therefore, once you refresh the non-root URL web page, nginx would return 404.
To solve this problem, just put try_files $uri $uri/ /index.html; at the last of location setting.
為什麼會看到廣告
9會員
49Content count
比起詳細教學單一技能,網路上或者市面上整合多項技能的教學相對少很多,但要真的完成一項專案往往不是只靠一項技能便能做到。 而且教科書式的完整教學也比較枯燥乏味,因此我想以自身的經驗為例,以全端開發各項技能中最常用到的部分進行講解,讓讀者能夠快速上手掌握全端技能。
留言0
查看全部
發表第一個留言支持創作者!
張哲嘉的沙龍 的其他內容
Authentication create root user In the mongo shell, create a root user to handle everything as follows. mongod.cfg connection Replication
I’m sure that I would forget it again after a few months, so let’s take note of it. Installation We need to use homebrew to install certbot. renew
Introduction There are some differences between deploying a front-end framework project on the web server and deploying a back-end one. uWSGI Nginx
Introduction Flutter is a powerful framework for developing the app for different platforms without redevelopment. main.dart route routing map
msix After comparing different methods, I think the most stable way is using msix to package the windows desktop app. Installation download
Introduction Set DDNS on Google Domains To set your DDNS record on Google Domains, just follow this document. ddclient installation ddclient.conf
Authentication create root user In the mongo shell, create a root user to handle everything as follows. mongod.cfg connection Replication
I’m sure that I would forget it again after a few months, so let’s take note of it. Installation We need to use homebrew to install certbot. renew
Introduction There are some differences between deploying a front-end framework project on the web server and deploying a back-end one. uWSGI Nginx
Introduction Flutter is a powerful framework for developing the app for different platforms without redevelopment. main.dart route routing map
msix After comparing different methods, I think the most stable way is using msix to package the windows desktop app. Installation download
Introduction Set DDNS on Google Domains To set your DDNS record on Google Domains, just follow this document. ddclient installation ddclient.conf
你可能也想看
Thumbnail
重點摘要: 1.9 月降息 2 碼、進一步暗示年內還有 50 bp 降息 2.SEP 上修失業率預期,但快速的降息速率將有助失業率觸頂 3.未來幾個月經濟數據將繼續轉弱,經濟復甦的時點或是 1Q25 季底附近
Thumbnail
近期的「貼文發佈流程 & 版型大更新」功能大家使用了嗎? 新版式整體視覺上「更加凸顯圖片」,為了搭配這次的更新,我們推出首次貼文策展 ❤️ 使用貼文功能並完成這次的指定任務,還有機會獲得富士即可拍,讓你的美好回憶都可以用即可拍珍藏!
• 學名:Ocimum basilicum • 使用部位:新鮮葉片、乾燥葉片 • 行星:火星 • 元素:火 • 魔法效用:意識心智、幸福、平靜、金錢 對於熟悉羅勒來自松子青醬的人來說,也許會對它背後豐富的歷史感到驚訝。在印度,過去的人會被埋葬在家中的地板下,人們會在窗戶上放置一罐羅勒,
Thumbnail
Zentange 20週年是一個值得慶祝的日子。為什麼禪繞是個有架構又彷彿無規則的藝術?它究竟為何神奇?一起來一探究竟!近期也有系列課程可以讓大家深入瞭解和體驗禪繞喔!
Thumbnail
本文為技術分析進階,適合有技術分析基礎者閱讀,不建議未學過基礎技術分析者閱讀
Thumbnail
習慣隨身帶本書,到了咖啡廳,坐下來後就能進入文字的世界裡不受干擾。來到南京東路的巷弄內,一家帶有工業風的復古咖啡廳,咖啡及甜點都到位,而且還吸引了韓國觀光客前來呢!拿出書來看,不經意的看了書籤背面,沒想到讓我眼睛為之一亮的,竟然是上頭的一段話⋯⋯
Thumbnail
大家好,進入研究所第一堂課就是上護理理論的課程~ 為什麼要學習護理理論呢?護理理論又是什麼呢?對於未來又有什麼幫助呢? 我想如果從五專開始接觸護理的同學肯定對於護理理論不陌生吧~
Thumbnail
重點摘要: 1.9 月降息 2 碼、進一步暗示年內還有 50 bp 降息 2.SEP 上修失業率預期,但快速的降息速率將有助失業率觸頂 3.未來幾個月經濟數據將繼續轉弱,經濟復甦的時點或是 1Q25 季底附近
Thumbnail
近期的「貼文發佈流程 & 版型大更新」功能大家使用了嗎? 新版式整體視覺上「更加凸顯圖片」,為了搭配這次的更新,我們推出首次貼文策展 ❤️ 使用貼文功能並完成這次的指定任務,還有機會獲得富士即可拍,讓你的美好回憶都可以用即可拍珍藏!
• 學名:Ocimum basilicum • 使用部位:新鮮葉片、乾燥葉片 • 行星:火星 • 元素:火 • 魔法效用:意識心智、幸福、平靜、金錢 對於熟悉羅勒來自松子青醬的人來說,也許會對它背後豐富的歷史感到驚訝。在印度,過去的人會被埋葬在家中的地板下,人們會在窗戶上放置一罐羅勒,
Thumbnail
Zentange 20週年是一個值得慶祝的日子。為什麼禪繞是個有架構又彷彿無規則的藝術?它究竟為何神奇?一起來一探究竟!近期也有系列課程可以讓大家深入瞭解和體驗禪繞喔!
Thumbnail
本文為技術分析進階,適合有技術分析基礎者閱讀,不建議未學過基礎技術分析者閱讀
Thumbnail
習慣隨身帶本書,到了咖啡廳,坐下來後就能進入文字的世界裡不受干擾。來到南京東路的巷弄內,一家帶有工業風的復古咖啡廳,咖啡及甜點都到位,而且還吸引了韓國觀光客前來呢!拿出書來看,不經意的看了書籤背面,沒想到讓我眼睛為之一亮的,竟然是上頭的一段話⋯⋯
Thumbnail
大家好,進入研究所第一堂課就是上護理理論的課程~ 為什麼要學習護理理論呢?護理理論又是什麼呢?對於未來又有什麼幫助呢? 我想如果從五專開始接觸護理的同學肯定對於護理理論不陌生吧~