当前位置 博文首页 > qq1113673178的博客:[笔记]Nginx使用及Windows/Linux部署

    qq1113673178的博客:[笔记]Nginx使用及Windows/Linux部署

    作者:[db:作者] 时间:2021-09-14 16:19

    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使用场景

    cs