Alex-Programer

Alex-Programer

随缘博客,不定期更新不确定的内容~
github
twitter

Nginx Configuration Notes

Introduction

This article only records the configurations that I believe are necessary to be recorded during my practice with nginx.

Configuring HTTPS

server {
  # Enable HTTPS access port
  listen 443 ssl;

  # Reference certificate
  ssl_certificate /etc/nginx/cert/xxx.com.pem;
  ssl_certificate_key /etc/nginx/cert/xxx.com.key;
}

Page Refresh 404

When the front-end router is in non-hash mode, this configuration can be used to solve this problem.

location / {
  try_files  $uri $uri/ /index.html;
  
  # If it is under the SSR web route, such as projects like Next.js, configure it like this.
  # try_files $uri $uri.html $uri/ =404;
}

Configuring 404 for SPA Subroutes
For example, www.xxx.com/admin is a subdomain.

location /admin {
  try_files  $uri $uri/ /admin/index.html;
}

Preventing Image Resource Theft

# Configuration for image resources
location ~* \.(gif|jpg|png|swf|flv)$ {
	valid_referers none blocked www.xxx.com xxx.com;

	if ($invalid_referer) {
		rewrite ^/ http://www.deepvps.com/retrun.html;
		#return 403; You can also not rewrite and return 403
	}
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.