[笔记]Nginx使用及Windows/Linux部署

20 篇文章 2 订阅

Web 基础——Nginx

Windows

在这里插入图片描述

使用方式

启动

nginx.exe

配置转发

端口转发

http 80->8080

进入conf
修改 nginx.conf
如果配置http的
找80端口 解开注释

server {
        listen       80;
        server_name  localhost;# 服务名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://127.0.0.1:8080;# 转发地址 可以是ip+端口
        }

https 443->8080

进入conf
修改 nginx.conf
如果配置https的
找443端口 解开注释

 # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  localhost;

        ssl_certificate      D:\tools\ngnix-1.12.2\nginx-1.12.2\ssl_aliyun\5115589_www.shiver.fun.pfx;
        ssl_certificate_key  D:\tools\ngnix-1.12.2\nginx-1.12.2\ssl_aliyun\pfx-password.txt;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://book_pool;
        }

注意:

PFX证书是window下面的证书,所以你Linux证书需要使用这个证书,我们需要把.PFX证书转换一下。我这里就转换成crt格式的了。

ssl_certificate_key需要证书的公钥
ssl_certificate需要.cert文件
都需要openssl生成

Nginx如何使用.PFX证书
关于解决nginx ssl问题处理

地址转发

常用命令

ngnix -s reload 
ngnix -s stop # 常常没用 需要手动 tasklist 然后 taskkill /pid /f
ngnix  #启动

Linux

nginx安装教程

完毕后
/usr/local/nginx
sbin里面有编译好的nginx
conf配置

/usr/local/nginx/nginx-1.9.9
objs里面有编译好的nginx
conf配置

记Nginx跨域问题

https://segmentfault.com/a/1190000020179829?utm_source=tag-newest

https://www.jianshu.com/p/520021853827

nginx方案

https://blog.csdn.net/zanpengfei/article/details/86605837
失败

https://cloud.tencent.com/developer/article/1648860
失败

后端方案

package team.shiver.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import team.shiver.handler.AuthenticationInterceptor;

@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/**");    // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
    }
    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }


    //跨域处理
    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){
        /**
         * 所有请求都允许跨域,使用这种配置就不需要
         * 在interceptor中配置header了
         */
        corsRegistry.addMapping("/**").
                allowedOrigins("*"). //允许跨域的域名,可以用*表示允许任何域名使用
                allowedMethods("*"). //允许任何方法(post、get等)
                allowedHeaders("*"). //允许任何请求头
                allowCredentials(true). //带上cookie信息
                exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
    }
}

可行

Nginx使用场景

1.代理转发

常见问题

1.访问网站某些资源网站提示 net::ERR_CONTENT_LENGTH_MISMATCH 200 (OK)

原因

查看nginx 错误日记 tail -f nginx/logs/error.log,显示 nginx/proxy_temp/ 目录 无权限。

解决方案

chmod -R 777 nginx/proxy_temp/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二进制怪兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值