专业编程基础技术教程

网站首页 > 基础教程 正文

nginx配置文件不知如何配置?|分享配置文件一键生成器

ccvgpt 2024-08-07 18:46:41 基础教程 10 ℃

概述

日常工作中,在部署好nginx之后,针对配置文件,大家往往有以下困惑。

  • 需要设置哪些参数?
  • 参数值需要设置成多少?
  • 有特定需求的,参数如何设置?如代理参数?ssl参数等。

下面分享一个配置文件一键生成神器,只要输入域名,选择相关选项,即可生成配置文件。

nginx配置文件不知如何配置?|分享配置文件一键生成器

地址: https://nginxconfig.io

NGINX Config 支持 HTTP、HTTPS、PHP、Python、Node.js、WordPress、Drupal、缓存、逆向代理、日志等各种配置选项。在线生成 Web 服务器 Nginx 配置文件。

操作配置也非常简单,你需要做的只需要2步:

  • 打开官方网站
  • 按需求配置相关参数

系统就会自动生成特定的配置文件。生成的Nginx格式非常规范。

案例展示

案例描述

  • 访问example.com时,直接跳转到https://example.com,完成http重定向。
  • 访问tomcat目录时,直接跳转到本机的8080端口。

生成的配置展示

1、生成主配置文件

cat /etc/nginx/sites-available/example.com.conf

server {
 listen 443 ssl http2;
 listen [::]:443 ssl http2;

 server_name example.com;
 root /var/www/example.com/var/www/abc.com;

 # SSL
 ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
 ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

 # security
 include nginxconfig.io/security.conf;

 # index.html fallback
 location / {
 try_files $uri $uri/ /index.html;
 }

 # reverse proxy
 location /tomcat {
 proxy_pass http://127.0.0.1:8080;
 include nginxconfig.io/proxy.conf;
 }

 # additional config
 include nginxconfig.io/general.conf;
}

# subdomains redirect
server {
 listen 443 ssl http2;
 listen [::]:443 ssl http2;

 server_name *.example.com;

 # SSL
 ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
 ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

 return 301 https://example.com$request_uri;
}

# HTTP redirect
server {
 listen 80;
 listen [::]:80;

 server_name .example.com;

 include nginxconfig.io/letsencrypt.conf;

 location / {
 return 301 https://example.com$request_uri;
 }
}

2、反向代理配置

cat /etc/nginx/nginxconfig.io/proxy.conf

proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;

3、其他nginx一般配置及安全配置



小结

1、通过上面的配置一个基础的nginx模板就生成了,具体的其他需求细节大家可以根据自己项目的实际情况,进行调整。

2、重要的还是要熟悉每个参数代表的意义,这样才能根据实际情况进行有针对性的调整,使用nginx性能达到最优。

最近发表
标签列表