一个简单的nginx配置

278 阅读1分钟

以下介绍了http和https的配置。

首先一级域名的http全部重定向到https;

server {
        listen       80;
        server_name  example.com; #一级域名
        rewrite ^ https://$http_host$request_uri? permanent;
}

设置web项目的域名、ssl正式位置以及配置、错误日志路径、默认页面文件路径。

server {
        listen 443 ssl;
    	ssl on;
    	server_name project.example.com; #二级域名指向页面
    	ssl_certificate /ssl/project.example.com.pem;
        ssl_certificate_key /ssl/project.example.com.key;
    	ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        access_log  /var/log/nginx/project.log  main;
    	location / {
    	   index index.html;
           root /home/root/project/dist;
        }
    }

设置server项目的域名、ssl证书位置以及配置、错误日志路径、端口配置。

 server {
        listen 80;
    	listen 443 ssl;
    	server_name api.example.com;
    	ssl_certificate /ssl/api.example.com.pem;
        ssl_certificate_key /ssl/api.example.com.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        access_log  /var/log/nginx/api.log  main;
        location / {
           proxy_redirect off;
           proxy_set_header Host $host;
           proxy_set_header X-Real-Ip $remote_addr;
           proxy_set_header X-Forwarded-For $remote_addr;
           proxy_pass http://localhost:3000;
        }
    }

然后即可通过http://project.example.com或者https://project.example.com来访问页面。