引言

这里我整理了几种web网站http配置文件的写法,仅供参考。

注意,具体要怎么进行配置,或者怎么设置结果,请移步另一篇博客内容

常规只允许http访问80端口

# 网站 http 访问
server{
    listen 80;
    server_name 域名;
    root 项目目录;
    index index.php index.html index.htm;

    # 所有请求全部重定向到index.php
    location / {
        if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
        }
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME 项目目录$fastcgi_script_name;
        include        fastcgi_params;
    }
}

注意,上面这里的配置内容需要修改一些东西:

  1. 域名
  2. 项目目录

如果你的项目比较特殊,也可以修改上面listen 80,监听对应的端口号,而不是我这里写的80端口。

还有就是这里的php-fpm服务端口是转发给到了9000端口,如果你的fpm是多版本或监听了其他端口,可以在这里修改

同时允许访问httphttps

# 网站 http 访问
server{
    listen 80;
    server_name 域名;
    root 项目目录;
    index index.php index.html index.htm;

    # 所有请求全部重定向到index.php
    location / {
        if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
        }
        if (!-f $request_filename){
            rewrite (.*) /index.php;
        }
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME 项目目录$fastcgi_script_name;
        include        fastcgi_params;
    }
}

# 网站 https 访问
server {
    listen    443 ssl;
    server_name 域名;
    root 项目目录;
    index index.php index.html index.htm;
    ssl_certificate 证书目录/域名.pem;
    ssl_certificate_key 证书目录/域名.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
    ssl_prefer_server_ciphers on;

    # 所有请求全部重定向到index.php
    location / {
        if (-f $request_filename/index.html){
            rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename/index.php){
            rewrite (.*) $1/index.php;
        }
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME 项目目录$fastcgi_script_name;
        include        fastcgi_params;
    }
}

我这里跟上面一样,也是需要修改一些你对应的配置信息的

  1. 域名
  2. 项目目录
  3. 证书目录

我这儿的证书文件是证书目录/域名.pem,你也可以按照你的喜好和放置位置进行设置。

阿里云下载的证书一般是个压缩包,,解压后的名称里带有域名,我就直接按照这种方式进行配置了。

http访问进来直接自动跳转到https

# 网站 http 访问
server{
    listen 80;
    server_name 域名;

    rewrite ^(.*)$  https://$host$1 permanent;
}

这里会产生一个302跳转,直接跳到https,但要注意,一定要配置好https的解析


欢迎关注拓行公众号,分享各种技术博客文章

拓行——奋勇进取,开拓未来,砥砺前行

最后修改:2024 年 04 月 25 日
如果您对各种技术博客文章感兴趣,欢迎关注拓行公众号,分享各种专业技术知识~