首页 > 系统相关 >Nginx常用配置

Nginx常用配置

时间:2023-12-26 18:13:51浏览次数:27  
标签:常用 http usr nginx 配置 Nginx location local proxy

项目配置示例

HTTP:

user  root;
worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout 1800s;     #指定 KeepAlive 的超时时间(timeout)。指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了      keepalive 连接。
    proxy_connect_timeout 1800s; #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_send_timeout 1800s; #后端服务器数据回传时间(代理发送超时)
    proxy_read_timeout 1800s; #连接成功后,后端服务器响应时间(代理接收超时)
    fastcgi_connect_timeout 1800s; #指定nginx与后端fastcgi server连接超时时间
    fastcgi_send_timeout 1800s; #指定nginx向后端传送请求超时时间(指已完成两次握手后向fastcgi传送请求超时时间)
    fastcgi_read_timeout 1800s; #指定nginx向后端传送响应超时时间(指已完成两次握手后向fastcgi传送响应超时时间)
    gzip  on;
       client_max_body_size 300m;
    server {
        listen       80;
        server_name  xxx.xxx.xxx.xxx;
            charset utf-8;
        #文件保存地址 location /xxxx{ alias /home/xxxx/uploadPath; } # 某某管理系统 location /xxxx/ { proxy_pass http://127.0.0.1:6500; } # 前端文件存放位置 location /yyyyy { alias /usr/local/nginx/html/yyyyy; try_files $uri $uri/ /yyyyy/index.html; index index.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

HTTPS:

1、验证是否安装ssl模块

/usr/local/nginx/sbin/nginx -v

 如果出现 (configure arguments: --with-http_ssl_module), 则已安装(下面的步骤可以跳过,直接进行第3步)。

2、安装ssl模块

# 进入到你的解压缩后的nginx目录,注意这里不是nginx安装目录,是解压缩后的目录
cd /usr/local/nginx-1.18.0

# 安装模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

#切记不要执行make install,否则会重新安装nginx
make

#停止nginx服务
 /usr/local/nginx/sbin/nginx -s stop
#替换之前的nginxcp /usr/local/nginx-1.18.0/objs/nginx /usr/local/nginx/sbin 

#注意这里是大写的V,小写的只显示版本号
/usr/local/nginx/sbin/nginx -V
#可以看到这里出现了configure arguments: --with-http_ssl_module 证明已经安装成功

#启动Nginx
/usr/local/nginx/sbin/nginx

 

 3、配置ssl证书

解压缩下载好的证书(证书一般是pem文件和key文件,这里名字可以随便改)

将下载好的证书上上传到服务器,我将证书放在了root目录下的card文件夹

# 创建存放证书路径
mkdir /usr/local/nginx/card

# 配置nginx.comf文件
vim /usr/local/nginx/conf/nginx.conf

配置如下:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
  server {
  #监听443端口
    listen 443;
    #你的域名
    server_name huiblog.top; 
    ssl on;
    #ssl证书的pem文件路径
    ssl_certificate  /usr/local/nginx/card/huiblog.top.pem;
    #ssl证书的key文件路径
    ssl_certificate_key /usr/local/nginx/card/huiblog.top.key;
    location / {
     proxy_pass  http://公网地址:项目端口号;
    }
}
server {
    listen 80;
    server_name huiblog.top;
    #将请求转成https
    rewrite ^(.*)$ https://$host$1 permanent;
}
}

4、重启Nginx服务

/usr/local/nginx/sbin/nginx -s reload

Nginx 之 proxy_pass详解

server {
    listen      80;
    server_name www.test.com;
 
    # 情形A
    # 访问 http://www.test.com/testa/aaaa
    # 后端的request_uri为: /testa/aaaa
    location ^~ /testa/ {
        proxy_pass http://127.0.0.1:8801;
    }
    
    # 情形B
    # 访问 http://www.test.com/testb/bbbb
    # 后端的request_uri为: /bbbb
    location ^~ /testb/ {
        proxy_pass http://127.0.0.1:8801/;
    }
 
    # 情形C
    # 下面这段location是正确的
    location ~ /testc {
        proxy_pass http://127.0.0.1:8801;
    }
 
    # 情形D
    # 下面这段location是错误的
    #
    # nginx -t 时,会报如下错误: 
    #
    # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular 
    # expression, or inside named location, or inside "if" statement, or inside 
    # "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
    # 
    # 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"
    location ~ /testd {
        proxy_pass http://127.0.0.1:8801/;   # 记住,location为正则表达式时,不能这样写!!!
    }
 
    # 情形E
    # 访问 http://www.test.com/ccc/bbbb
    # 后端的request_uri为: /aaa/ccc/bbbb
    location /ccc/ {
        proxy_pass http://127.0.0.1:8801/aaa$request_uri;
    }
 
    # 情形F
    # 访问 http://www.test.com/namea/ddd
    # 后端的request_uri为: /yongfu?namea=ddd
    location /namea/ {
        rewrite    /namea/([^/]+) /yongfu?namea=$1 break;
        proxy_pass http://127.0.0.1:8801;
    }
 
    # 情形G
    # 访问 http://www.test.com/nameb/eee
    # 后端的request_uri为: /yongfu?nameb=eee
    location /nameb/ {
        rewrite    /nameb/([^/]+) /yongfu?nameb=$1 break;
        proxy_pass http://127.0.0.1:8801/;
    }
 
    access_log /data/logs/www/www.test.com.log;
}
 
server {
    listen      8801;
    server_name www.test.com;
    
    root        /data/www/test;
    index       index.php index.html;
 
    rewrite ^(.*)$ /test.php?u=$1 last;
 
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
 
    access_log /data/logs/www/www.test.com.8801.log;
}
 

 

标签:常用,http,usr,nginx,配置,Nginx,location,local,proxy
From: https://www.cnblogs.com/liaozk/p/17928940.html

相关文章

  • ArgoCD notifications 配置
    ArgoCDnotifications TriggersTriggers定义应发送通知的条件。定义包括名称、条件和通知模板参考。条件是一个谓词表达式,如果通知则返回true应该发送。Triggers列表1.on-created#Application创建2.on-deleted#Applic......
  • Spring应用最常用的工具类汇总
    文件资源访问假设有一个文件地址位于Web应用的类路径下,您可以通过以下方式对这个文件资源进行访问:FileSystemResource以文件系统绝对路径的方式进行访问;ClassPathResource以类路径的方式进行访问;ServletContextResource以相对于Web应用根目录的方式进行访问。Resourc......
  • 验证码技术指南:在线对“验证魔方”进行个性化配置
    验证码作为人机交互界面经常出现的关键要素,是身份核验、防范风险、数据反爬的重要组成部分,广泛应用网站、App上,在注册、登录、交易、交互等各类场景中发挥着巨大作用,具有真人识别、身份核验的功能,在保障账户安全方面也具有重要作用。顶象无感验证对操作者进行智能判定,将操作者区......
  • nginx部署vue编译项目刷新页面404
    原因:nginx配置错误。因为vue打包输出的是单页网页应用,只有一个index.html入口,其他路径是由前端路由去跳转的,服务器目录下没有对应物理路径,所以就会报404。这样的nginx配置会出现该问题。location/{alias/home/vue/dist/;indexindex.html;}  解......
  • QT 中配置 64位kafka ,c++
    在MSYS2下,执行$pacman-Smingw32/mingw-w64-i686-librdkafkamingw64/mingw-w64-x86_64-librdkafka即可获得二进制库、头文件和动态链接库。文件路径实例,D:\msys64\mingw64下找文件即可:D:\msys64\mingw64\lib\librdkafka++.dll.a 在工程文件中创建文件夹thirdparty/librdkaf......
  • Taurus .Net Core 微服务开源框架:Admin 插件【4-6】 - 配置管理-Mvc【Plugin-Doc 接口
    前言:继上篇:Taurus.NetCore微服务开源框架:Admin插件【4-5】-配置管理-Mvc【Plugin-Admin后台】本篇继续介绍下一个内容:系统配置节点:Mvc- Plugin- Doc 接口测试及文档:配置界面如下:  配置说明如下:1、Doc.IsEnable:配置当前接口测试文档插件是否可用这是一个......
  • 如何配置双网关/双网卡
    #1.前期准备1)两个网卡都需要正确设置,包括各自默认网关(注意,两个网卡都需要设定网关)  网卡配置文件在/etc/sysconfig/network-scripts/目录下,需要正确设置两个网卡的配置文件,然后重新启用新配置。2)两个网卡都已正确接入到两路线路,并且两路网络的服务供应商都已正常提供服务#2.......
  • grep -Ev '#|^$' zabbix_server.conf 显示配置文件 命令
    grep-Ev'#|^$'zabbix_server.confroot@k8s-node01:/etc/zabbix#grep-Ev'#|^$'zabbix_server.confLogFile=/var/log/zabbix/zabbix_server.logLogFileSize=0PidFile=/run/zabbix/zabbix_server.pidSocketDir=/run/zabbixDBName=zabbixDBUser=......
  • Nginx反向代理MQTT服务端(emqx)
    安装Nginx此处使用Ubuntu22.04LTS系统,通过源码编译安装的方式安装Nginx。你也可以使用Docker或二进制包安装Nginx。环境要求在编译和安装Nginx前,需要确保系统中已经安装了以下依赖项:GNUC和C++编译器PCRE(PerlCompatibleRegularExpressions)库zlib压缩库OpenSSL......
  • 2、安装nginx
    1、检查Linux内核版本uname-a内核版本为3.10.0,Linux安装需要内核版本>=2.62、安装依赖环境安装gcc如果已经安装过可以忽略yuminstallgcc安装g++如果已经安装过可以忽略yuminstallgcc-c++安装PCRE库(perlcompatibleregularexpression)如果已经安......