Linux中Nginx的HTTP和HTTPS常用配置以及proxy_pass详解

这篇具有很好参考价值的文章主要介绍了Linux中Nginx的HTTP和HTTPS常用配置以及proxy_pass详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

Linux中Nginx的HTTP和HTTPS常用配置以及proxy_pass详解,Linux服务器中间件安装部署,http,linux,nginx
如果出现 (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

配置如下:文章来源地址https://www.toymoban.com/news/detail-789821.html

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;
}

到了这里,关于Linux中Nginx的HTTP和HTTPS常用配置以及proxy_pass详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Nginx】location中的root、proxy_pass和alias

    root、proxy_pass和alias都是用来配置Nginx服务器上的URL重写的指令 1.1 root指令 root :用于指定文件系统中某个目录作为请求的根目录,Nginx会在该目录下寻找请求的文件。例如: 上述配置使得访问 /images/ 目录下的资源时,Nginx会到 /var/www/images/ 目录下查找。 最后请求的路径:ro

    2024年02月10日
    浏览(12)
  • nginx 常用配置之 pass_proxy

    大家好,我是 17。 今天和大家聊聊 pass_proxy 代理。 在前端代理主要是为了跨域。虽然前端跨域有多种方法,各有利弊,但用代理来跨域对开发是最友好的。用代理可以不用修改产品代码切换线上线下,非常安全。pass_proxy 默认会把 cookie 也一同转发。 常用的配置非常简单。

    2024年02月13日
    浏览(11)
  • 轻松配置 https:Let‘s Encrypt 介绍及 Nginx Proxy Manager 实用操作教程

    一般我们在本地进行 web 开发时用的都是 http 协议,而部署到服务器上之后为了安全都要配置 https,以保证客户端和服务器之间的通信内容得到加密,不会被泄露或篡改。 本文将介绍 https 协议的基本知识(如果有基础可以跳过),以及如何使用 Let’s Encrypt 给你的服务优雅地

    2024年03月11日
    浏览(17)
  • nginx支持一个端口访问多个前端项目(http以及https)

        最近做项目结构优化,前端项目都是部署在nginx上,想实现同一个端口可以访问多个前端项目.这样可以提高服务器的端口复用率,降低项目部署以及维护成本.根据平常的需求,用两台nginx服务器分别支持http、https同一端口访问不同项目。下面将配置方式以及相关注意事项做简

    2024年02月03日
    浏览(25)
  • Nginx配置http跳转https

    Nginx 可通过多种方式实现 http 跳转 https,以下列出各种方式的实现方法。 这是 Nginx 新版本的写法,推荐使用。在 Nginx 80 监听服务上加一行: 完整配置如下: Nginx 老版本的写法,不推荐使用。将 “#http跳转https” 这行修改为: 或 将 “#http跳转https” 这行修改为: 写一个

    2024年02月13日
    浏览(17)
  • Nginx配置http和https

    配置文件 默认放置位置:{nginx}/conf.d/,以conf结尾 一、http简单配置 说明: 1,http默认端口是80 2,http://127.0.0.1:8888;为实际本地服务端口 3,一般服务域名为二级域名www,一级域名一般也配置指向www域名。 二、https配置 首先得申请ssl证书,百度,阿里都有免费证书可用,申请成

    2023年04月09日
    浏览(10)
  • nginx配置http强制跳转https

    一、什么是Nginx? Nginx是一个高性能的HTTP和反向代理Web服务器,同时也提供IMAP/POP3/SMTP服务。Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3/SMTP)代理服务器。Nginx的特点是:占有内存少,并发能力强。 Nginx专门为性能优化而开发,性能是最重要的考量,非常

    2024年02月16日
    浏览(18)
  • 【nginx】配置将HTTPS请求转换成HTTP

    要将HTTPS请求转换为HTTP请求,可以在Nginx的配置文件中添加以下配置: 打开Nginx的配置文件,通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf 。 在 server 块中添加以下配置,将HTTPS请求转发到后端的HTTP服务: 将 yourdomain.com 替换为你的域名, /path/to/your/ssl_certificate.crt 和

    2024年02月10日
    浏览(15)
  • nginx配置http请求转成https请求

    1、return 301 2、rewitre 3、error_page 原理: http和https是tcp的上层协议,当nginx服务器建立tcp连接后,根据收到的第一份数据来确定客户端是希望建立tls还是http。nginx会判断tcp请求的首写节内容以进行区分,如果是0x80或者0x16就可能是ssl或者tls,然后尝试https握手。如果端口开启了

    2024年02月07日
    浏览(18)
  • Nginx 配置https以及wss

    可以在阿里云申请免费ssl证书,一年更换一次 注意: 1、配置完成后nginx需要重启,reload证书是不会生效的 2、如需要支持wss协议,需要增加配置: proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection \\\"upgrade\\\"; 3、配置https之后,最后就禁用掉http,通过如下配置,强制跳转https

    2024年02月11日
    浏览(16)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包