Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

记一次完整简单的nginx配置过程,不想每次配置再东谷歌西百度了。 #9

Open
zp1112 opened this issue May 9, 2018 · 2 comments
Labels

Comments

@zp1112
Copy link
Owner

zp1112 commented May 9, 2018

安装nginx

简单粗暴点的

sudo apt-get install nginx

默认位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
默认使用/etc/nginx/conf.d/*.conf的配置,以后写nginx代理都放在conf.d目录下面。
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志

配置nginx

简单粗暴,cd /etc/nginx/conf.d/

cd /etc/nginx/conf.d/
vi docs.conf

server {
    listen 80;
    server_name docs.icodin.cn;
    rewrite ^(.*) https://$server_name$1 permanent; // 自动从http跳转到https
}
server{
    listen      443 ssl;  // 端口出来443还可以是其他端口,访问时加上端口号即可,同时开启ssl
    server_name docs.icodin.cn; // 可以使用localhost也可以使用自己的域名,记得将dns记录指向你的服务器ip
    charset     utf-8;
    client_max_body_size 75M;
    ssl_certificate      /etc/nginx/conf.d/candy.crt; // 等会会生成的证书
    ssl_certificate_key  /etc/nginx/conf.d/candy.key;
    ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
      root /home/ubuntu/web/docs/;  // 将你的静态文件放在这个目录,默认渲染index.html或者index.htm
    }
}

生成证书

简单粗暴,在/etc/nginx/conf.d目录下面执行,反正遇到permission deny啥的直接加sudo就好了。

openssl genrsa -des3 -out candy.key 1024 // 生成私钥
openssl req -new -key candy.key -out candy.csr // 生成证书签名请求
openssl rsa -in candy.key-out candy.key // 移除私钥的密码
openssl x509 -req -days 365 -in candy.csr -signkey candy.key -out candy.crt // 生成证书
ls
candy.crt  candy.csr  candy.key

拉项目

将你的静态页面上传或者github拉取(ssh啥的自己配),反正各种自己的方式把你的静态文件放到刚刚配置的nginx里面的root文件夹里,/home/ubuntu/web/,比如我将docs项目放到了web文件下。

还是记一下吧(ssh啥的)

ssh-keygen
cat ~/.ssh/id_rsa.pub
复制粘贴到github的sshkey配置里面

ok,最后一步,执行

nginx -s reload

浏览器打开 https://docs.icodin.cn (或者你自己配的域名,或者直接ip访问),boom!大功告成。可算是放到一起了,不用再这里谷歌一下怎么生存自签名,哪里谷歌一下怎么配置nginx的ssl了,毕竟老是记不得的。。

@zp1112 zp1112 added the nginx label May 9, 2018
@blackmatch
Copy link

静态文件应该是放在/usr/share/nginx/html目录下吧?难道ubuntu系统不一样?

@zp1112
Copy link
Owner Author

zp1112 commented May 10, 2018

@blackmatch 是滴,感谢大佬指出

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants