`
Jameslyy
  • 浏览: 385068 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Nginx 安装和配置

阅读更多

环境:centos

 

sudo yum install -y nginx

 

systemctl start nginx.service

systemctl enable nginx.service

systemctl status nginx

 

配置文件 nginx.conf

 

验证配置和加载配置

nginx -t

nginx -s reload 

 

 

服务配置

 

    server {

        listen       443 ssl;

        server_name  xxx.com;

        access_log   /data/logs/nginx/apitest.log;

        root         /usr/share/nginx/html;

 

        include /etc/nginx/default.d/*.conf;

 

        # ssl证书

        ssl_certificate /data/config/nginx/xx.pem;

        ssl_certificate_key /data/config/nginx/xx.key;

 

        ssl_session_timeout 5m;

 

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

        ssl_prefer_server_ciphers on;

 

        location / {

        # 允许跨域的源,可以使用 * 来允许所有源,也可以指定具体的域名,不建议使用 * $http_origin

        add_header 'Access-Control-Allow-Origin' '$http_origin' always;

        # 允许的跨域请求方法,这里要包括 OPTIONS

        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT' always;

        # 允许的请求头

        add_header 'Access-Control-Allow-Headers' 'x-requested-with, Content-Type, origin, authorization, accept, client-security-token' always;

        # 允许携带 Cookie 跨域

        add_header 'Access-Control-Allow-Credentials' 'true' always;

 

        # 预检请求的有效期,单位秒

        add_header 'Access-Control-Max-Age' 3600;

        if ($request_method = OPTIONS) {

            # 对于 OPTIONS 请求直接返回 200

            return 200;

        }

            proxy_pass          http://backend_x;

            proxy_redirect      off;

            proxy_set_header    Host               $host;

            proxy_set_header    X-Real-IP          $remote_addr;

            proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;

        }

 

 

        error_page 404 /404.html;

        location = /404.html {

        }

 

        error_page 500 502 503 504 /50x.html;

        location = /50x.html {

        }

 

    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics